[comp.lang.lisp.x] Complex/Rational math package, Patch2

comas@math.lsa.umich.edu (Ray Comas) (12/12/90)

This is another patch for my complex/rational number package. 
Currently, the real and imaginary parts of complex numbers are stored
as part of a "struct node".  This means that every node requires ca.
18 bytes of space, which is almost twice as much as previously required.
This patch addresses that problem.  The real and imaginary parts are now
kept in a dynamically allocated array (much like the STRING type), which
means nodes are now back to 9--10 bytes, just like they used to be.

The changes are to dldmem.c, dlimage.c, and xldmem.h

Note that you will have to come up with your own fixes to xldmem.c and
xlimage.c if you use these instead of dldmem.c and dlimage.c

Also, if there are any functions or features that you would like to see
added to this package, let me know.

============ CUT HERE ============ CUT HERE ============ CUT HERE ============
diff +context orig/dldmem.c ./dldmem.c
*** orig/dldmem.c	Mon Dec 03 17:28:32 1990
--- ./dldmem.c	Tue Dec 11 17:22:34 1990
***************
*** 330,338 ****
  	LVAL val;
  
  	if (i==0.0) return cvflonum(r);
! 	val = newnode(COMPLEX);
! 	val->n_complex.xc_real = r;
! 	val->n_complex.xc_imag = i;
  	return val;
  }
  #endif
--- 330,339 ----
  	LVAL val;
  
  	if (i==0.0) return cvflonum(r);
! 	val = allocvector(COMPLEX,btow_size(2*sizeof(FLOTYPE)));
! 	val->n_vsize = 2*sizeof(FLOTYPE);
! 	val->n_complex.xc_data[0] = r;
! 	val->n_complex.xc_data[1] = i;
  	return val;
  }
  #endif
***************
*** 803,811 ****
--- 804,822 ----
  	while (vdata < vfree) {
  		vector = *vdata;
  #ifdef JGC
+ #ifdef COMPLEX_TYPES
+ 		if ((vector->n_type & TYPEFIELD)==STRING
+ 			|| (vector->n_type & TYPEFIELD)==COMPLEX)
+ #else
  		if ((vector->n_type & TYPEFIELD) == STRING)
+ #endif
  #else
+ #ifdef COMPLEX_TYPES
+ 		if (vector->n_type==STRING
+ 			|| vector->n_type==COMPLEX)
+ #else
  		if (vector->n_type == STRING)
+ #endif
  #endif
  			vsize = btow_size(vector->n_vsize) + 1;
  		else
diff +context orig/dlimage.c ./dlimage.c
*** orig/dlimage.c	Tue Dec 11 17:26:36 1990
--- ./dlimage.c	Tue Dec 11 17:25:48 1990
***************
*** 124,129 ****
--- 124,132 ----
  				for (i = 0; i < max; ++i)
  					writeptr(cvoptr(getelement(p,i)));
  				break;
+ #ifdef COMPLEX_TYPES
+ 			case COMPLEX:
+ #endif
  			case STRING:
  				max = getslength(p);
  				fwrite(getstring(p),1,max,fp);
***************
*** 227,232 ****
--- 230,238 ----
  				for (i = 0; i < max; ++i)
  					setelement(p,i,cviptr(readptr()));
  				break;
+ #ifdef COMPLEX_TYPES
+ 			case COMPLEX:
+ #endif
  			case STRING:
  				max = getslength(p);
  				p->n_string = (char *)getvspace(p,btow_size(max));
diff +context orig/xldmem.h ./xldmem.h
*** orig/xldmem.h	Mon Dec 03 16:13:02 1990
--- ./xldmem.h	Tue Dec 11 16:36:30 1990
***************
*** 79,86 ****
  #ifdef COMPLEX_TYPES
  #define getrnumer(x)	((x)->n_rational.xr_num)
  #define getrdenom(x)	((x)->n_rational.xr_den)
! #define getcreal(x)	((x)->n_complex.xc_real)
! #define getcimag(x)	((x)->n_complex.xc_imag)
  #endif
  
  /* string access macros */
--- 79,86 ----
  #ifdef COMPLEX_TYPES
  #define getrnumer(x)	((x)->n_rational.xr_num)
  #define getrdenom(x)	((x)->n_rational.xr_den)
! #define getcreal(x)	((x)->n_complex.xc_data[0])
! #define getcimag(x)	((x)->n_complex.xc_data[1])
  #endif
  
  /* string access macros */
***************
*** 223,229 ****
  			FIXTYPE xr_num, xr_den;		/* numerator & denominator */
  		} n_xrational;
  		struct xcomplex {		/* complex node */
! 			FLOTYPE xc_real, xc_imag;	/* real & imaginary parts */
  		} n_xcomplex;
  #endif
  		struct xstring {		/* string node */
--- 223,230 ----
  			FIXTYPE xr_num, xr_den;		/* numerator & denominator */
  		} n_xrational;
  		struct xcomplex {		/* complex node */
! 			int xc_dummy;
! 			FLOTYPE *xc_data;	/* real & imaginary parts */
  		} n_xcomplex;
  #endif
  		struct xstring {		/* string node */
***************
*** 308,314 ****
  extern LVAL cvflonum();		/* convert a flonum */
  #ifdef COMPLEX_TYPES
  extern LVAL cvrational();	/* convert a rational */
! extern LVAL cvcomplex()		/* convert a complex */
  #endif
  extern LVAL newstring();		/* create a new string */
  extern LVAL newvector();		/* create a new vector */
--- 309,315 ----
  extern LVAL cvflonum();		/* convert a flonum */
  #ifdef COMPLEX_TYPES
  extern LVAL cvrational();	/* convert a rational */
! extern LVAL cvcomplex();	/* convert a complex */
  #endif
  extern LVAL newstring();		/* create a new string */
  extern LVAL newvector();		/* create a new vector */
============ CUT HERE ============ CUT HERE ============ CUT HERE ============
Ray Comas (comas@math.lsa.umich.edu)
Department of mathematics
University of Michigan