Max 5 API Reference
00001 /* 00002 Copyright 2001-2005 - Cycling '74 00003 Joshua Kit Clayton jkc@cycling74.com 00004 */ 00005 00006 #include "jit.common.h" 00007 #include "jit.fixmath.h" 00008 00009 #define JIT_LA_DETERMINANT_DEF_THRESH 0.000000001 00010 00011 typedef struct _jit_la_determinant 00012 { 00013 t_object ob; 00014 double thresh; 00015 long resultcount; 00016 double result[2]; 00017 } t_jit_la_determinant; 00018 00019 void *_jit_la_determinant_class; 00020 00021 t_jit_la_determinant *jit_la_determinant_new(void); 00022 void jit_la_determinant_free(t_jit_la_determinant *x); 00023 t_jit_err jit_la_determinant_matrix_calc(t_jit_la_determinant *x, void *inputs, void *outputs); 00024 00025 t_jit_err jit_la_determinant_init(void) 00026 { 00027 long attrflags=0; 00028 t_jit_object *attr,*mop; 00029 00030 _jit_la_determinant_class = jit_class_new("jit_la_determinant",(method)jit_la_determinant_new,(method)jit_la_determinant_free, 00031 sizeof(t_jit_la_determinant),0L); 00032 00033 //add mop 00034 mop = jit_object_new(_jit_sym_jit_mop,1,0); 00035 jit_class_addadornment(_jit_la_determinant_class,mop); 00036 //add methods 00037 jit_class_addmethod(_jit_la_determinant_class, (method)jit_la_determinant_matrix_calc, "matrix_calc", A_CANT, 0L); 00038 //add attributes 00039 attrflags = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW; 00040 attr = jit_object_new(_jit_sym_jit_attr_offset,"thresh",_jit_sym_float64,attrflags, 00041 (method)0L,(method)0L,calcoffset(t_jit_la_determinant,thresh)); 00042 jit_class_addattr(_jit_la_determinant_class,attr); 00043 attrflags = JIT_ATTR_SET_OPAQUE_USER | JIT_ATTR_GET_OPAQUE_USER; 00044 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"result",_jit_sym_float64,2,attrflags, 00045 (method)0L,(method)0L,calcoffset(t_jit_la_determinant,resultcount),calcoffset(t_jit_la_determinant,result)); 00046 jit_class_addattr(_jit_la_determinant_class,attr); 00047 00048 jit_class_register(_jit_la_determinant_class); 00049 00050 return JIT_ERR_NONE; 00051 } 00052 00053 t_jit_err jit_la_determinant_matrix_calc(t_jit_la_determinant *x, void *inputs, void *outputs) 00054 { 00055 t_jit_err err=JIT_ERR_NONE; 00056 long tmp_savelock; 00057 t_jit_matrix_info in_minfo,tmp_minfo; 00058 char *tmp_bp; 00059 long swapcount=0; 00060 void *tmp_matrix=NULL; 00061 void *uppertri=NULL; 00062 void *diag=NULL; 00063 double *det=x->result; 00064 t_atom *av=NULL; 00065 void *in_matrix; 00066 00067 in_matrix = jit_object_method(inputs,_jit_sym_getindex,0); 00068 00069 if (x&&in_matrix) { 00070 det[0] = 0; 00071 det[1] = 0; 00072 jit_object_method(in_matrix,_jit_sym_getinfo,&in_minfo); 00073 //compatible types? 00074 if ((in_minfo.type!=_jit_sym_float32)&&(in_minfo.type!=_jit_sym_float64)) 00075 { err=JIT_ERR_MISMATCH_TYPE; goto out;} 00076 //compatible planecounts? 00077 //planecount 1=real. planecount 2=complex 00078 if (((in_minfo.planecount!=1)&&(in_minfo.planecount!=2))) 00079 { err=JIT_ERR_MISMATCH_PLANE; goto out;} 00080 x->resultcount = in_minfo.planecount; 00081 //square? 00082 if (in_minfo.dim[0]!=in_minfo.dim[1]) 00083 { err=JIT_ERR_MISMATCH_DIM; goto out;} 00084 //calculate upper triangle 00085 if (!(tmp_matrix=jit_object_new(_jit_sym_jit_matrix,&in_minfo))) 00086 { err=JIT_ERR_OUT_OF_MEM; goto out;} 00087 if (!(uppertri=jit_object_new(gensym("jit_la_uppertri")))) 00088 { err=JIT_ERR_OUT_OF_MEM; goto out;} 00089 jit_attr_setfloat(uppertri,gensym("thresh"),x->thresh); 00090 00091 if (err=(long)jit_object_method(uppertri,_jit_sym_matrix_calc,inputs,tmp_matrix)) //TRICKY: single matrix acting as single element list 00092 goto out; 00093 00094 swapcount = jit_attr_getlong(uppertri,gensym("swapcount")); 00095 //calculate diagonal product 00096 if (!(diag=jit_object_new(gensym("jit_la_diagproduct")))) 00097 { err=JIT_ERR_OUT_OF_MEM; goto out;} 00098 00099 if (err=(long)jit_object_method(diag,_jit_sym_matrix_calc,tmp_matrix,NULL)) //TRICKY: single matrix acting as single element list 00100 goto out; 00101 00102 jit_object_method(diag,gensym("getresult"),&x->resultcount,&av); 00103 det[0] = jit_atom_getfloat(av); 00104 det[1] = jit_atom_getfloat(av+1); 00105 00106 if (av) jit_freebytes(av,x->resultcount*sizeof(t_atom)); 00107 00108 //if odd number of row swaps, then negate 00109 if (swapcount%2) { det[0] *= -1; det[1] *= -1; } 00110 00111 } else { 00112 return JIT_ERR_INVALID_PTR; 00113 } 00114 00115 out: 00116 if (tmp_matrix) { 00117 jit_object_free(tmp_matrix); 00118 } 00119 if (uppertri) jit_object_free(uppertri); 00120 if (diag) jit_object_free(diag); 00121 return err; 00122 } 00123 00124 t_jit_la_determinant *jit_la_determinant_new(void) 00125 { 00126 t_jit_la_determinant *x; 00127 00128 if (x=(t_jit_la_determinant *)jit_object_alloc(_jit_la_determinant_class)) { 00129 x->thresh = JIT_LA_DETERMINANT_DEF_THRESH; 00130 x->resultcount=1; 00131 x->result[0]=0; 00132 x->result[1]=0; 00133 } else { 00134 x = NULL; 00135 } 00136 return x; 00137 } 00138 00139 void jit_la_determinant_free(t_jit_la_determinant *x) 00140 { 00141 }
Copyright © 2008, Cycling '74