Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!utnut!cs.utexas.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!darwin.sura.net!bogus.sura.net!news-feed-1.peachnet.edu!umn.edu!csus.edu!sfsuvax1.sfsu.edu!vpcsc4
From: vpcsc4@sfsuvax1.sfsu.edu (Emmett McLean)
Subject: Bug calling J script from C
Message-ID: <1993Apr30.000925.6177@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
References: <1993Apr27.210112.2780@csus.edu> <1685@kepler1.rentec.com> <1993Apr29.233311.4264@csus.edu>
Date: Fri, 30 Apr 1993 00:09:25 GMT
Lines: 76


Can anyone explain this?

Here's a J script for the file hermite.js :

   hi =. , (-/@(,&0@(<:@+:@# * ]) ,: 0&,)@{: - ,&0@(*:@<:@# * ])@{:@}:)
   hm =. 'hi ^:(<:@<: x.) y.':11
   5 hm (2 2 $ 1 0 1 _1) 


And when I load it from an interative session of J it works perfectly :

J6.2   Copyright (c) 1990-1992, Iverson Software Inc.  All Rights Reserved.

   0!:2 <'hermite.js'
   hi =. , (-/@(,&0@(<:@+:@# * ]) ,: 0&,)@{: - ,&0@(*:@<:@# * ])@{:@}:)
   hm =. 'hi ^:(<:@<: x.) y.':11
   5 hm (2 2 $ 1 0 1 _1) 
 1   0  0   0 0
 1  _1  0   0 0
 2  _4  1   0 0
 6 _18  9  _1 0
24 _96 72 _16 1
   
But when it is loaded via 0!:2 (or 0!:3) from a C program there's a value error.

   e>%j
hi =. , (-/@(,&0@(<:@+:@# * ]) ,: 0&,)@{: - ,&0@(*:@<:@# * ])@{:@}:)
hm =. 'hi ^:(<:@<: x.) y.':11
5 hm (2 2 $ 1 0 1 _1) 
value error

value error

Segmentation fault (core dumped)
e>%

Here is the C program :

#include <stdio.h>
#include <string.h>
#include "lj.h"

#define INDEX(y,i,j) (AV(y)[(i)*AS(y)[1]+(j)])

C jc(k,f1,f2)I k;AF*f1,*f2;{}

main(){A p; int i,j; int e[5][5];
 jinit();
 
 /*
 load J script :
 */
 (void) jx(" 0!:2 <'/usr/s3/emclean/j/src/hermite.js' ");

 /*
 assign p the A struct containing the 1st 5 hermite polynomials :
 */
 p = jx(" 5 hm (2 2 $ 1 0 1 _1) ");


 /*
 prints values of p and demonstrates 
 assignment into a array of shape int[][] 
 */

 for(i=0;i<5;i++){
 printf("\n");
  for(j=0;j<5;j++){
   e[i][j] = INDEX(p,i,j);
   printf(" %5d",INDEX(p,i,j));
  }
 }
 printf("\n");
}

