version 1.1, 2015/08/04 05:24:44 |
version 1.2, 2015/08/06 09:15:32 |
|
|
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/ox_pari/ox_pari.c,v 1.1 2015/08/04 05:24:44 noro Exp $ */ |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
#include "pari/pari.h" |
#include "gmp.h" |
#include "gmp.h" |
|
#include "gmp-impl.h" |
#include "ox_toolkit.h" |
#include "ox_toolkit.h" |
OXFILE *fd_rw; |
OXFILE *fd_rw; |
|
|
Line 11 static int stack_size = 0; |
|
Line 13 static int stack_size = 0; |
|
static int stack_pointer = 0; |
static int stack_pointer = 0; |
static cmo **stack = NULL; |
static cmo **stack = NULL; |
extern int debug_print; |
extern int debug_print; |
|
long paristack=10000000; |
|
|
void init_pari(void); |
void init_pari(void); |
|
cmo *GEN_to_cmo(GEN z); |
|
cmo_zz *GEN_to_cmo_zz(GEN z); |
|
cmo_list *GEN_to_cmo_list(GEN z); |
|
GEN cmo_to_GEN(cmo *c); |
|
GEN cmo_zz_to_GEN(cmo_zz *c); |
|
|
#define INIT_S_SIZE 2048 |
#define INIT_S_SIZE 2048 |
#define EXT_S_SIZE 2048 |
#define EXT_S_SIZE 2048 |
|
|
void init_pari() |
void init_pari() |
{ |
{ |
|
pari_init(paristack,2); |
} |
} |
|
|
int initialize_stack() |
int initialize_stack() |
|
|
|
|
int sm_mathcap() |
int sm_mathcap() |
{ |
{ |
mathcap_init(OX_PARI_VERSION, ID_STRING, "ox_cdd", NULL, NULL); |
mathcap_init(OX_PARI_VERSION, ID_STRING, "ox_pari", NULL, NULL); |
push((cmo*)oxf_cmo_mathcap(fd_rw)); |
push((cmo*)oxf_cmo_mathcap(fd_rw)); |
return 0; |
return 0; |
} |
} |
|
|
|
|
cmo_error2 *make_error2(int code) |
cmo_error2 *make_error2(int code) |
{ |
{ |
return (cmo_error2 *) new_cmo_int32(-1); |
return (cmo_error2 *) new_cmo_int32(code); |
} |
} |
|
|
int get_i() |
int get_i() |
Line 128 int cmo2int(cmo *c) |
|
Line 137 int cmo2int(cmo *c) |
|
return 0; |
return 0; |
} |
} |
|
|
|
GEN cmo_zz_to_GEN(cmo_zz *c) |
|
{ |
|
mpz_ptr mpz; |
|
GEN z; |
|
long *ptr; |
|
int j,sgn,len; |
|
|
|
mpz = c->mpz; |
|
sgn = mpz_sgn(mpz); |
|
len = ABSIZ(mpz); |
|
ptr = (long *)PTR(mpz); |
|
z = cgeti(len+2); |
|
for ( j = 0; j < len; j++ ) |
|
z[len-j+1] = ptr[j]; |
|
setsigne(z,sgn); |
|
setlgefint(z,lg(z)); |
|
return z; |
|
} |
|
|
|
cmo_zz *GEN_to_cmo_zz(GEN z) |
|
{ |
|
cmo_zz *c; |
|
|
|
c = new_cmo_zz(); |
|
mpz_import(c->mpz,lgef(z)-2,1,sizeof(long),0,0,&z[2]); |
|
if ( signe(z) < 0 ) |
|
mpz_neg(c->mpz,c->mpz); |
|
return c; |
|
} |
|
|
|
cmo_list *GEN_to_cmo_list(GEN z) |
|
{ |
|
cmo_list *c; |
|
cmo *ob; |
|
int i,len; |
|
|
|
c = new_cmo_list(); |
|
len = lg(z)-1; |
|
for ( i = 1; i <= len; i++ ) { |
|
ob = GEN_to_cmo((GEN)z[i]); |
|
c = list_append(c,ob); |
|
} |
|
return c; |
|
} |
|
|
|
|
|
GEN cmo_to_GEN(cmo *c) |
|
{ |
|
switch ( c->tag ) { |
|
case CMO_ZERO: |
|
case CMO_ZZ: /* int */ |
|
return cmo_zz_to_GEN((cmo_zz *)c); |
|
default: |
|
return 0; |
|
} |
|
} |
|
|
|
cmo *GEN_to_cmo(GEN z) |
|
{ |
|
if ( gcmp0(z) ) |
|
return new_cmo_zero(); |
|
switch ( typ(z) ) { |
|
case 1: /* int */ |
|
return (cmo *)GEN_to_cmo_zz(z); |
|
case 17: case 18: /* vector */ |
|
return (cmo *)GEN_to_cmo_list(z); |
|
case 19: /* matrix */ |
|
return (cmo *)GEN_to_cmo_list(shallowtrans(z)); |
|
default: |
|
return (cmo *)make_error2(typ(z)); |
|
} |
|
} |
|
|
|
|
|
#define PARI_MAX_AC 64 |
|
|
int sm_executeFunction() |
int sm_executeFunction() |
{ |
{ |
|
int ac,i; |
|
cmo_int32 *c; |
|
cmo *av[PARI_MAX_AC]; |
|
cmo *ret; |
|
GEN z,m; |
|
|
cmo_string *func = (cmo_string *)pop(); |
cmo_string *func = (cmo_string *)pop(); |
if(func->tag != CMO_STRING) { |
if(func->tag != CMO_STRING) { |
printf("sm_executeFunction : func->tag is not CMO_STRING");fflush(stdout); |
printf("sm_executeFunction : func->tag is not CMO_STRING");fflush(stdout); |
Line 137 int sm_executeFunction() |
|
Line 228 int sm_executeFunction() |
|
return -1; |
return -1; |
} |
} |
|
|
if(strcmp(func->s, "factor" ) == 0) { |
c = (cmo_int32 *)pop(); |
printf("afo\n");fflush(stdout); |
ac = c->i; |
|
if ( ac > PARI_MAX_AC ) { |
|
push((cmo*)make_error2(0)); |
|
return -1; |
|
} |
|
for ( i = 0; i < ac; i++ ) { |
|
av[i] = (cmo *)pop(); |
|
fprintf(stderr,"arg%d:",i); |
|
print_cmo(av[i]); |
|
fprintf(stderr,"\n"); |
|
} |
|
if(strcmp(func->s, "factor") == 0) { |
|
z = cmo_to_GEN(av[0]); |
|
m = Z_factor(z); |
|
ret = GEN_to_cmo(m); |
|
push(ret); |
return 0; |
return 0; |
|
} else if(strcmp(func->s, "nextprime") == 0) { |
|
z = cmo_to_GEN(av[0]); |
|
m = nextprime(z); |
|
ret = GEN_to_cmo(m); |
|
push(ret); |
|
return 0; |
|
} else if(strcmp(func->s, "det") == 0) { |
|
z = cmo_to_GEN(av[0]); |
|
m = det(z); |
|
ret = GEN_to_cmo(m); |
|
push(ret); |
|
return 0; |
} else if( strcmp( func->s, "exit" ) == 0 ){ |
} else if( strcmp( func->s, "exit" ) == 0 ){ |
pop(); |
pop(); |
exit(0); |
exit(0); |
|
|
tag = receive_ox_tag(fd_rw); |
tag = receive_ox_tag(fd_rw); |
switch(tag) { |
switch(tag) { |
case OX_DATA: |
case OX_DATA: |
printf("receive : ox_data\n",tag);fflush(stdout); |
printf("receive : ox_data %d\n",tag);fflush(stdout); |
push(receive_cmo(fd_rw)); |
push(receive_cmo(fd_rw)); |
break; |
break; |
case OX_COMMAND: |
case OX_COMMAND: |
printf("receive : ox_command\n",tag);fflush(stdout); |
printf("receive : ox_command %d\n",tag);fflush(stdout); |
receive_and_execute_sm_command(); |
receive_and_execute_sm_command(); |
break; |
break; |
default: |
default: |