Update of /cvsroot/pure-data/externals/iem/iemmatrix/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9964
Modified Files:
iemmatrix.c iemmatrix.h mtx_binops.c mtx_col.c mtx_element.c
mtx_inverse.c mtx_matrix.c mtx_rand.c mtx_row.c
mtx_transpose.c mtx_zeros.c
Added Files:
mtx_add.c mtx_mul.c mtx_pow.c mtx_sub.c
Log Message:
split the objects from mtx_binops into several files: mtx_add, mtx_sub, mtx_mul, mtx_pow
mtx_binops is still there for glue functions
exposed some important operations on float-arrays via iemmatrix.h (mtx_doInvert, mtx_doTranspose, mtx_doMultiply)
as you can easily see, they all start with "mtx_do" and a capital letter;
they all return a pointer to (newly allocated) memory with the result
Index: mtx_row.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_row.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_row.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_row.c 11 May 2005 13:05:29 -0000 1.3
***************
*** 115,119 ****
! void iemmtx_row_setup(void)
{
mtx_row_setup();
--- 115,119 ----
! void iemtx_row_setup(void)
{
mtx_row_setup();
Index: mtx_col.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_col.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_col.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_col.c 11 May 2005 13:05:28 -0000 1.3
***************
*** 124,128 ****
}
! void iemmtx_col_setup(void)
{
mtx_col_setup();
--- 124,128 ----
}
! void iemtx_col_setup(void)
{
mtx_col_setup();
Index: mtx_matrix.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_matrix.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_matrix.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_matrix.c 11 May 2005 13:05:29 -0000 1.3
***************
*** 598,602 ****
}
! void iemmtx_matrix_setup(void){
matrix_setup();
}
--- 598,602 ----
}
! void iemtx_matrix_setup(void){
matrix_setup();
}
Index: mtx_element.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_element.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_element.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_element.c 11 May 2005 13:05:29 -0000 1.3
***************
*** 113,117 ****
! void iemmtx_element_setup(void)
{
mtx_element_setup();
--- 113,117 ----
! void iemtx_element_setup(void)
{
mtx_element_setup();
Index: mtx_zeros.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_zeros.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_zeros.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_zeros.c 11 May 2005 13:05:29 -0000 1.3
***************
*** 52,56 ****
class_sethelpsymbol(mtx_zeros_class, gensym("iemmatrix/mtx_special"));
}
! void iemmtx_zeros_setup(void){
mtx_zeros_setup();
}
--- 52,56 ----
class_sethelpsymbol(mtx_zeros_class, gensym("iemmatrix/mtx_special"));
}
! void iemtx_zeros_setup(void){
mtx_zeros_setup();
}
--- NEW FILE: mtx_mul.c ---
/*
* iemmatrix
*
* objects for manipulating simple matrices
* mostly refering to matlab/octave matrix functions
*
* Copyright (c) IOhannes m zmölnig, forum::für::umläute
* IEM, Graz, Austria
*
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*
*/
#include "iemmatrix.h"
/*
mtx_mul
mtx_*
mtx_.*
mtx_./
matrix multiplication
*/
t_matrixfloat*mtx_doMultiply(int rowA, t_matrixfloat*A, int colArowB, t_matrixfloat*B, int colB){
t_matrixfloat*result=(t_matrixfloat*)getbytes(sizeof(t_matrixfloat)*rowA*colB);
int r, c, n;
for(r=0; r<rowA; r++){
for(c=0; c<colB; c++){
t_matrixfloat sum=0.f;
for(n=0;n<colArowB; n++)
sum+=A[colArowB*r+n]*B[colB*n+c];
}
}
return result;
}
/* mtx_mul */
static t_class *mtx_mul_class, *mtx_mulelement_class, *mtx_mulscalar_class;
static void mtx_mul_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
{
t_matrix *m=&x->m, *m2=&x->m2;
t_atom *ap, *ap1=argv+2, *ap2=m2->atombuffer+2;
int row=atom_getfloat(argv), col=atom_getfloat(argv+1);
int row2, col2, n, r, c;
if (!m2->atombuffer){ post("mulitply with what ?"); return; }
if (argc<2){ post("mtx_mul: crippled matrix"); return; }
if ((col<1)||(row<1)){post("mtx_mul: invalid dimensions"); return; }
if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
row2=atom_getfloat(m2->atombuffer);
col2=atom_getfloat(m2->atombuffer+1);
if (col!=row2) { post("mtx_mul: matrix dimensions do not match !"); return; }
adjustsize(m, row, col2);
ap=m->atombuffer+2;
for(r=0;r<row;r++)
for(c=0;c<col2;c++) {
t_matrixfloat sum = 0.f;
for(n=0;n<col;n++)sum+=(t_matrixfloat)atom_getfloat(ap1+col*r+n)*atom_getfloat(ap2+col2*n+c);
SETFLOAT(ap+col2*r+c,sum);
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
}
static void mtx_mul_float(t_mtx_binmtx *x, t_float f)
{
t_matrix *m=&x->m, *m2=&x->m2;
t_atom *ap, *ap2=m2->atombuffer+2;
int row2, col2, n;
if (!m2->atombuffer){ post("mulitply with what ?"); return; }
row2=atom_getfloat(m2->atombuffer);
col2=atom_getfloat(m2->atombuffer+1);
adjustsize(m, row2, col2);
ap=m->atombuffer+2;
n=row2*col2;
while(n--){
SETFLOAT(ap, f*atom_getfloat(ap2++));
ap++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
}
static void mtx_mulelement_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
{
int row=atom_getfloat(argv++);
int col=atom_getfloat(argv++);
t_atom *m;
t_atom *m2 = x->m2.atombuffer+2;
int n = argc-2;
if (argc<2){ post("mtx_mul: crippled matrix"); return; }
if ((col<1)||(row<1)) { post("mtx_mul: invalid dimensions"); return; }
if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
if (!(x->m2.col*x->m2.row)) {
adjustsize(&x->m, row, col);
matrix_set(&x->m, 0);
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
return;
}
if ((col!=x->m2.col)||(row!=x->m2.row)){ post("matrix dimension do not match"); /* LATER SOLVE THIS */ return; }
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
t_float f = atom_getfloat(argv++)*atom_getfloat(m2++);
SETFLOAT(m, f);
m++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_mulscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc-2;
t_atom *m;
t_float factor = x->f;
int row=atom_getfloat(argv++);
int col=atom_getfloat(argv++);
if (argc<2){
post("mtx_mul: crippled matrix");
return;
}
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = atom_getfloat(argv++)*factor;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_mulscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc;
t_atom *m;
t_float factor = x->f;
adjustsize(&x->m, 1, argc);
m = x->m.atombuffer;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = atom_getfloat(argv++)*factor;
}
outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
}
static void *mtx_mul_new(t_symbol *s, int argc, t_atom *argv)
{
if (argc>1) post("mtx_mul : extra arguments ignored");
if (argc) {
t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_mulscalar_class);
floatinlet_new(&x->x_obj, &x->f);
x->f = atom_getfloatarg(0, argc, argv);
outlet_new(&x->x_obj, 0);
return(x);
} else {
if (s->s_name[4]=='.') {
/* element mul */
t_matrix *x = (t_matrix *)pd_new(mtx_mulelement_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
outlet_new(&x->x_obj, 0);
x->col = x->row = 0;
x->atombuffer = 0;
return(x);
} else {
t_mtx_binmtx *x = (t_mtx_binmtx *)pd_new(mtx_mul_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
outlet_new(&x->x_obj, 0);
x->m.col = x->m.row = x->m2.col = x->m2.row = 0;
x->m.atombuffer = x->m2.atombuffer = 0;
return (x);
}
}
}
void mtx_mul_setup(void)
{
mtx_mul_class = class_new(gensym("mtx_mul"), (t_newmethod)mtx_mul_new, (t_method)mtx_binmtx_free,
sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
class_addcreator((t_newmethod)mtx_mul_new, gensym("mtx_*"), A_GIMME,0);
class_addmethod(mtx_mul_class, (t_method)mtx_mul_matrix, gensym("matrix"), A_GIMME, 0);
class_addmethod(mtx_mul_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
class_addfloat (mtx_mul_class, mtx_mul_float);
class_addbang (mtx_mul_class, mtx_binmtx_bang);
mtx_mulelement_class = class_new(gensym("mtx_.*"), (t_newmethod)mtx_mul_new, (t_method)mtx_binmtx_free,
sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
class_addmethod(mtx_mulelement_class, (t_method)mtx_mulelement_matrix, gensym("matrix"), A_GIMME, 0);
class_addmethod(mtx_mulelement_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
class_addfloat (mtx_mulelement_class, mtx_mul_float);
class_addbang (mtx_mulelement_class, mtx_binmtx_bang);
mtx_mulscalar_class = class_new(gensym("mtx_mul"), 0, (t_method)mtx_binscalar_free,
sizeof(t_mtx_binscalar), 0, 0);
class_addcreator(0, gensym("mtx_*"), 0, 0);
class_addcreator(0, gensym("mtx_.*"), 0, 0);
class_addmethod(mtx_mulscalar_class, (t_method)mtx_mulscalar_matrix, gensym("matrix"), A_GIMME, 0);
class_addlist (mtx_mulscalar_class, mtx_mulscalar_list);
class_addbang (mtx_mulscalar_class, mtx_binscalar_bang);
class_sethelpsymbol(mtx_mul_class, gensym("iemmatrix/mtx_binops"));
class_sethelpsymbol(mtx_mulelement_class, gensym("iemmatrix/mtx_binops"));
class_sethelpsymbol(mtx_mulscalar_class, gensym("iemmatrix/mtx_binops"));
}
/* mtx_div */
static t_class *mtx_divelement_class, *mtx_divscalar_class;
static void mtx_divelement_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
{
int row=atom_getfloat(argv++);
int col=atom_getfloat(argv++);
t_atom *m;
t_atom *m2 = x->m2.atombuffer+2;
int n = argc-2;
if (argc<2){ post("mtx_div: crippled matrix"); return; }
if ((col<1)||(row<1)) { post("mtx_div: invalid dimensions"); return; }
if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
if (!(x->m2.col*x->m2.row)) {
adjustsize(&x->m, row, col);
matrix_set(&x->m, 0);
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
return;
}
if ((col!=x->m2.col)||(row!=x->m2.row)){ post("matrix dimension do not match"); /* LATER SOLVE THIS */ return; }
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
t_float f = atom_getfloat(argv++)/atom_getfloat(m2++);
SETFLOAT(m, f);
m++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_divelement_float(t_mtx_binmtx *x, t_float f)
{
t_matrix *m=&x->m, *m2=&x->m2;
t_atom *ap, *ap2=m2->atombuffer+2;
int row2, col2, n;
if (!m2->atombuffer){ post("divide by what ?"); return; }
row2=atom_getfloat(m2->atombuffer);
col2=atom_getfloat(m2->atombuffer+1);
adjustsize(m, row2, col2);
ap=m->atombuffer+2;
n=row2*col2;
while(n--){
SETFLOAT(ap, f/atom_getfloat(ap2++));
ap++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
}
static void mtx_divscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc-2;
t_atom *m;
t_float factor = 1.0/x->f;
int row=atom_getfloat(argv++);
int col=atom_getfloat(argv++);
if (argc<2){
post("mtx_div: crippled matrix");
return;
}
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = atom_getfloat(argv++)*factor;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_divscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc;
t_atom *m;
t_float factor = 1.0/x->f;
adjustsize(&x->m, 1, argc);
m = x->m.atombuffer;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = atom_getfloat(argv++)*factor;
}
outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
}
static void *mtx_div_new(t_symbol *s, int argc, t_atom *argv)
{
if (argc>1) post("mtx_div : extra arguments ignored");
if (argc) {
/* scalar division */
t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_divscalar_class);
floatinlet_new(&x->x_obj, &x->f);
x->f = atom_getfloatarg(0, argc, argv);
outlet_new(&x->x_obj, 0);
return(x);
} else {
/* element division */
t_matrix *x = (t_matrix *)pd_new(mtx_divelement_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
outlet_new(&x->x_obj, 0);
x->col = x->row = 0;
x->atombuffer = 0;
return(x);
}
}
void mtx_div_setup(void)
{
mtx_divelement_class = class_new(gensym("mtx_./"), (t_newmethod)mtx_div_new, (t_method)mtx_binmtx_free,
sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
class_addmethod(mtx_divelement_class, (t_method)mtx_divelement_matrix, gensym("matrix"), A_GIMME, 0);
class_addmethod(mtx_divelement_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
class_addfloat (mtx_divelement_class, mtx_divelement_float);
class_addbang (mtx_divelement_class, mtx_binmtx_bang);
mtx_divscalar_class = class_new(gensym("mtx_./"), 0, (t_method)mtx_binscalar_free,
sizeof(t_mtx_binscalar), 0, 0);
class_addmethod(mtx_divscalar_class, (t_method)mtx_divscalar_matrix, gensym("matrix"), A_GIMME, 0);
class_addlist (mtx_divscalar_class, mtx_divscalar_list);
class_addbang (mtx_divscalar_class, mtx_binscalar_bang);
class_sethelpsymbol(mtx_divelement_class, gensym("iemmatrix/mtx_binops"));
class_sethelpsymbol(mtx_divscalar_class, gensym("iemmatrix/mtx_binops"));
}
void iemtx_mul_setup(void)
{
mtx_mul_setup();
mtx_div_setup();
}
--- NEW FILE: mtx_sub.c ---
/*
* iemmatrix
*
* objects for manipulating simple matrices
* mostly refering to matlab/octave matrix functions
*
* Copyright (c) IOhannes m zmölnig, forum::für::umläute
* IEM, Graz, Austria
*
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*
*/
#include "iemmatrix.h"
/* -------------------------------------------------------------- */
/*
* mtx_sub
* mtx_-
*/
static t_class *mtx_sub_class, *mtx_subscalar_class;
static void mtx_subscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc-2;
int row=atom_getfloat(argv), col=atom_getfloat(argv+1);
t_float offset=x->f;
t_atom *buf;
t_atom *ap=argv+2;
if(argc<2){post("mtx_sub: crippled matrix");return; }
adjustsize(&x->m, row, col);
buf=x->m.atombuffer+2;
while(n--){
buf->a_type = A_FLOAT;
buf++->a_w.w_float = atom_getfloat(ap++) - offset;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_subscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc;
t_atom *m;
t_float offset = x->f;
adjustsize(&x->m, 1, argc);
m = x->m.atombuffer;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = atom_getfloat(argv++) - offset;
}
outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
}
static void mtx_sub_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
{
int row=atom_getfloat(argv);
int col=atom_getfloat(argv+1);
t_atom *m;
t_atom *m1 = argv+2;
t_atom *m2 = x->m2.atombuffer+2;
int n = argc-2;
if (argc<2){ post("mtx_sub: crippled matrix"); return; }
if ((col<1)||(row<1)) { post("mtx_sub: invalid dimensions"); return; }
if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
if (!(x->m2.col*x->m2.row)) {
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, argv);
return;
}
if ((col!=x->m2.col)||(row!=x->m2.row)){
post("mtx_sub: matrix dimensions do not match");
/* LATER SOLVE THIS */
return;
}
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
t_float f = atom_getfloat(m1++)-atom_getfloat(m2++);
SETFLOAT(m, f);
m++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_sub_float(t_mtx_binmtx *x, t_float f)
{
t_matrix *m=&x->m, *m2=&x->m2;
t_atom *ap, *ap2=m2->atombuffer+2;
int row2, col2, n;
if (!m2->atombuffer){ post("mulitply with what ?"); return; }
row2=atom_getfloat(m2->atombuffer);
col2=atom_getfloat(m2->atombuffer+1);
adjustsize(m, row2, col2);
ap=m->atombuffer+2;
n=row2*col2;
while(n--){
SETFLOAT(ap, f-atom_getfloat(ap2++));
ap++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
}
static void *mtx_sub_new(t_symbol *s, int argc, t_atom *argv)
{
if (argc>1) post("mtx_sub : extra arguments ignored");
if (argc) {
t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_subscalar_class);
floatinlet_new(&x->x_obj, &x->f);
x->f = atom_getfloatarg(0, argc, argv);
outlet_new(&x->x_obj, 0);
return(x);
} else {
t_mtx_binmtx *x = (t_mtx_binmtx *)pd_new(mtx_sub_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
outlet_new(&x->x_obj, 0);
x->m.col = x->m.row = x->m2.col = x->m2.row = 0;
x->m.atombuffer = x->m2.atombuffer = 0;
return(x);
}
}
void mtx_sub_setup(void)
{
mtx_sub_class = class_new(gensym("mtx_sub"), (t_newmethod)mtx_sub_new, (t_method)mtx_binmtx_free,
sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
class_addcreator((t_newmethod)mtx_sub_new, gensym("mtx_-"), A_GIMME,0);
class_addmethod(mtx_sub_class, (t_method)mtx_sub_matrix, gensym("matrix"), A_GIMME, 0);
class_addmethod(mtx_sub_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
class_addfloat (mtx_sub_class, mtx_sub_float);
class_addbang (mtx_sub_class, mtx_binmtx_bang);
mtx_subscalar_class = class_new(gensym("mtx_sub"), 0, (t_method)mtx_binscalar_free,
sizeof(t_mtx_binscalar), 0, 0);
class_addcreator(0, gensym("mtx_-"), 0, 0);
class_addmethod(mtx_subscalar_class, (t_method)mtx_subscalar_matrix, gensym("matrix"), A_GIMME, 0);
class_addlist (mtx_subscalar_class, mtx_subscalar_list);
class_addbang (mtx_subscalar_class, mtx_binscalar_bang);
class_sethelpsymbol(mtx_sub_class, gensym("iemmatrix/mtx_binops"));
class_sethelpsymbol(mtx_subscalar_class, gensym("iemmatrix/mtx_binops"));
}
void iemtx_sub_setup(void)
{
mtx_sub_setup();
}
Index: mtx_transpose.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_transpose.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_transpose.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_transpose.c 11 May 2005 13:05:29 -0000 1.3
***************
*** 17,20 ****
--- 17,32 ----
static t_class *mtx_transpose_class;
+ t_matrixfloat*mtx_doTranspose(t_matrixfloat*transposee, int row, int col){
+ int r,c;
+ t_matrixfloat*transposed=(t_matrixfloat*)getbytes(sizeof(t_matrixfloat)*row*col);
+ r=row;
+ while(r--){
+ c=col;
+ while(c--)
+ transposed[c*row+r]=transposee[r*col+c];
+ }
+ return transposed;
+ }
+
static void mtx_transpose_matrix(t_matrix *x, t_symbol *s, int argc, t_atom *argv)
{
Index: mtx_rand.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_rand.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_rand.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_rand.c 11 May 2005 13:05:29 -0000 1.3
***************
*** 88,92 ****
}
! void iemmtx_rand_setup(void){
mtx_rand_setup();
}
--- 88,92 ----
}
! void iemtx_rand_setup(void){
mtx_rand_setup();
}
--- NEW FILE: mtx_pow.c ---
/*
* iemmatrix
*
* objects for manipulating simple matrices
* mostly refering to matlab/octave matrix functions
*
* Copyright (c) IOhannes m zmölnig, forum::für::umläute
* IEM, Graz, Austria
*
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*
*/
#include "iemmatrix.h"
/* mtx_.^ */
/* LATER: do a mtx_pow, mtx_^ */
#ifdef __APPLE__
/* there is no such thing like powf on apple... */
static inline float powf (float v, float p)
{
return (float) pow((double)v, (double) p);
}
#endif /* __APPLE__ */
static t_class *mtx_powelement_class, *mtx_powscalar_class;
static void mtx_powelement_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
{
int row=atom_getfloat(argv++);
int col=atom_getfloat(argv++);
t_atom *m;
t_atom *m2 = x->m2.atombuffer+2;
int n = argc-2;
if (argc<2){ post("mtx_pow: crippled matrix"); return; }
if ((col<1)||(row<1)) { post("mtx_pow: invalid dimensions"); return; }
if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
if (!(x->m2.col*x->m2.row)) {
adjustsize(&x->m, row, col);
matrix_set(&x->m, 0);
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
return;
}
if ((col!=x->m2.col)||(row!=x->m2.row)){ post("matrix dimension do not match"); /* LATER SOLVE THIS */ return; }
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
t_float f = powf(atom_getfloat(argv++),atom_getfloat(m2++));
SETFLOAT(m, f);
m++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_powelement_float(t_mtx_binmtx *x, t_float f)
{
t_matrix *m=&x->m, *m2=&x->m2;
t_atom *ap, *ap2=m2->atombuffer+2;
int row2, col2, n;
if (!m2->atombuffer){ post("power by what ?"); return; }
row2=atom_getfloat(m2->atombuffer);
col2=atom_getfloat(m2->atombuffer+1);
adjustsize(m, row2, col2);
ap=m->atombuffer+2;
n=row2*col2;
while(n--){
SETFLOAT(ap, powf(f,atom_getfloat(ap2++)));
ap++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
}
static void mtx_powscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc-2;
t_atom *m;
t_float factor = x->f;
int row=atom_getfloat(argv++);
int col=atom_getfloat(argv++);
if (argc<2){
post("mtx_pow: crippled matrix");
return;
}
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = powf(atom_getfloat(argv++),factor);
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_powscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc;
t_atom *m;
t_float factor = x->f;
adjustsize(&x->m, 1, argc);
m = x->m.atombuffer;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = powf(atom_getfloat(argv++),factor);
}
outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
}
static void *mtx_pow_new(t_symbol *s, int argc, t_atom *argv)
{
if (argc>1) post("mtx_pow : extra arguments ignored");
if (argc) {
/* scalar powision */
t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_powscalar_class);
floatinlet_new(&x->x_obj, &x->f);
x->f = atom_getfloatarg(0, argc, argv);
outlet_new(&x->x_obj, 0);
return(x);
} else {
/* element powision */
t_matrix *x = (t_matrix *)pd_new(mtx_powelement_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
outlet_new(&x->x_obj, 0);
x->col = x->row = 0;
x->atombuffer = 0;
return(x);
}
}
void mtx_pow_setup(void)
{
mtx_powelement_class = class_new(gensym("mtx_.^"), (t_newmethod)mtx_pow_new, (t_method)mtx_binmtx_free,
sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
class_addmethod(mtx_powelement_class, (t_method)mtx_powelement_matrix, gensym("matrix"), A_GIMME, 0);
class_addmethod(mtx_powelement_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
class_addfloat (mtx_powelement_class, mtx_powelement_float);
class_addbang (mtx_powelement_class, mtx_binmtx_bang);
mtx_powscalar_class = class_new(gensym("mtx_.^"), 0, (t_method)mtx_binscalar_free,
sizeof(t_mtx_binscalar), 0, 0);
class_addmethod(mtx_powscalar_class, (t_method)mtx_powscalar_matrix, gensym("matrix"), A_GIMME, 0);
class_addlist (mtx_powscalar_class, mtx_powscalar_list);
class_addbang (mtx_powscalar_class, mtx_binscalar_bang);
class_sethelpsymbol(mtx_powelement_class, gensym("iemmatrix/mtx_binops"));
class_sethelpsymbol(mtx_powscalar_class, gensym("iemmatrix/mtx_binops"));
}
void iemtx_pow_setup(void)
{
mtx_pow_setup();
}
Index: mtx_binops.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_binops.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** mtx_binops.c 10 May 2005 09:37:23 -0000 1.5
--- mtx_binops.c 11 May 2005 13:05:28 -0000 1.6
***************
*** 14,26 ****
#include "iemmatrix.h"
- /*
- mtx_add
- mtx_+
- mtx_mul
- mtx_*
- mtx_.*
- mtx_./
- */
-
/* -------------------------------------------------------------- */
/* matrix math */
--- 14,17 ----
***************
*** 63,809 ****
matrix_free(&x->m);
}
-
-
-
- /* mtx_add */
- static t_class *mtx_add_class, *mtx_addscalar_class;
-
- static void mtx_addscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc-2;
- int row=atom_getfloat(argv), col=atom_getfloat(argv+1);
-
- t_float offset=x->f;
- t_atom *buf;
- t_atom *ap=argv+2;
-
- if(argc<2){post("mtx_add: crippled matrix");return; }
- adjustsize(&x->m, row, col);
-
- buf=x->m.atombuffer+2;
-
- while(n--){
- buf->a_type = A_FLOAT;
- buf++->a_w.w_float = atom_getfloat(ap++) + offset;
- }
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_addscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc;
- t_atom *m;
- t_float offset = x->f;
- adjustsize(&x->m, 1, argc);
- m = x->m.atombuffer;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = atom_getfloat(argv++) + offset;
- }
- outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
- }
-
- static void mtx_add_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
- {
- int row=atom_getfloat(argv);
- int col=atom_getfloat(argv+1);
- t_atom *m;
- t_atom *m1 = argv+2;
- t_atom *m2 = x->m2.atombuffer+2;
- int n = argc-2;
-
- if (argc<2){ post("mtx_add: crippled matrix"); return; }
- if ((col<1)||(row<1)) { post("mtx_add: invalid dimensions"); return; }
- if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
-
- if (!(x->m2.col*x->m2.row)) {
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, argv);
- return;
- }
-
- if ((col!=x->m2.col)||(row!=x->m2.row)){
- post("mtx_add: matrix dimensions do not match");
- /* LATER SOLVE THIS */
- return;
- }
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- t_float f = atom_getfloat(m1++)+atom_getfloat(m2++);
- SETFLOAT(m, f);
- m++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_add_float(t_mtx_binmtx *x, t_float f)
- {
- t_matrix *m=&x->m, *m2=&x->m2;
- t_atom *ap, *ap2=m2->atombuffer+2;
- int row2, col2, n;
-
- if (!m2->atombuffer){ post("mulitply with what ?"); return; }
-
- row2=atom_getfloat(m2->atombuffer);
- col2=atom_getfloat(m2->atombuffer+1);
- adjustsize(m, row2, col2);
- ap=m->atombuffer+2;
-
- n=row2*col2;
-
- while(n--){
- SETFLOAT(ap, f+atom_getfloat(ap2++));
- ap++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
- }
- static void *mtx_add_new(t_symbol *s, int argc, t_atom *argv)
- {
- if (argc>1) post("mtx_add : extra arguments ignored");
- if (argc) {
- t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_addscalar_class);
- floatinlet_new(&x->x_obj, &x->f);
- x->f = atom_getfloatarg(0, argc, argv);
- outlet_new(&x->x_obj, 0);
- return(x);
- } else {
- t_mtx_binmtx *x = (t_mtx_binmtx *)pd_new(mtx_add_class);
- inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
- outlet_new(&x->x_obj, 0);
- x->m.col = x->m.row = x->m2.col = x->m2.row = 0;
- x->m.atombuffer = x->m2.atombuffer = 0;
- return(x);
- }
- }
-
- static void mtx_add_setup(void)
- {
- mtx_add_class = class_new(gensym("mtx_add"), (t_newmethod)mtx_add_new, (t_method)mtx_binmtx_free,
- sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
- class_addcreator((t_newmethod)mtx_add_new, gensym("mtx_+"), A_GIMME,0);
- class_addmethod(mtx_add_class, (t_method)mtx_add_matrix, gensym("matrix"), A_GIMME, 0);
- class_addmethod(mtx_add_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
- class_addfloat (mtx_add_class, mtx_add_float);
- class_addbang (mtx_add_class, mtx_binmtx_bang);
-
- mtx_addscalar_class = class_new(gensym("mtx_add"), 0, (t_method)mtx_binscalar_free,
- sizeof(t_mtx_binscalar), 0, 0);
- class_addcreator(0, gensym("mtx_+"), 0, 0);
- class_addmethod(mtx_addscalar_class, (t_method)mtx_addscalar_matrix, gensym("matrix"), A_GIMME, 0);
- class_addlist (mtx_addscalar_class, mtx_addscalar_list);
- class_addbang (mtx_addscalar_class, mtx_binscalar_bang);
-
- class_sethelpsymbol(mtx_add_class, gensym("iemmatrix/mtx_binops"));
- class_sethelpsymbol(mtx_addscalar_class, gensym("iemmatrix/mtx_binops"));
- }
-
- /* mtx_sub */
- static t_class *mtx_sub_class, *mtx_subscalar_class;
-
- static void mtx_subscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc-2;
- int row=atom_getfloat(argv), col=atom_getfloat(argv+1);
-
- t_float offset=x->f;
- t_atom *buf;
- t_atom *ap=argv+2;
-
- if(argc<2){post("mtx_sub: crippled matrix");return; }
- adjustsize(&x->m, row, col);
-
- buf=x->m.atombuffer+2;
-
- while(n--){
- buf->a_type = A_FLOAT;
- buf++->a_w.w_float = atom_getfloat(ap++) - offset;
- }
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_subscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc;
- t_atom *m;
- t_float offset = x->f;
- adjustsize(&x->m, 1, argc);
- m = x->m.atombuffer;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = atom_getfloat(argv++) - offset;
- }
- outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
- }
-
- static void mtx_sub_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
- {
- int row=atom_getfloat(argv);
- int col=atom_getfloat(argv+1);
- t_atom *m;
- t_atom *m1 = argv+2;
- t_atom *m2 = x->m2.atombuffer+2;
- int n = argc-2;
-
- if (argc<2){ post("mtx_sub: crippled matrix"); return; }
- if ((col<1)||(row<1)) { post("mtx_sub: invalid dimensions"); return; }
- if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
-
- if (!(x->m2.col*x->m2.row)) {
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, argv);
- return;
- }
-
- if ((col!=x->m2.col)||(row!=x->m2.row)){
- post("mtx_sub: matrix dimensions do not match");
- /* LATER SOLVE THIS */
- return;
- }
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- t_float f = atom_getfloat(m1++)-atom_getfloat(m2++);
- SETFLOAT(m, f);
- m++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_sub_float(t_mtx_binmtx *x, t_float f)
- {
- t_matrix *m=&x->m, *m2=&x->m2;
- t_atom *ap, *ap2=m2->atombuffer+2;
- int row2, col2, n;
-
- if (!m2->atombuffer){ post("mulitply with what ?"); return; }
-
- row2=atom_getfloat(m2->atombuffer);
- col2=atom_getfloat(m2->atombuffer+1);
- adjustsize(m, row2, col2);
- ap=m->atombuffer+2;
-
- n=row2*col2;
-
- while(n--){
- SETFLOAT(ap, f-atom_getfloat(ap2++));
- ap++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
- }
- static void *mtx_sub_new(t_symbol *s, int argc, t_atom *argv)
- {
- if (argc>1) post("mtx_sub : extra arguments ignored");
- if (argc) {
- t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_subscalar_class);
- floatinlet_new(&x->x_obj, &x->f);
- x->f = atom_getfloatarg(0, argc, argv);
- outlet_new(&x->x_obj, 0);
- return(x);
- } else {
- t_mtx_binmtx *x = (t_mtx_binmtx *)pd_new(mtx_sub_class);
- inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
- outlet_new(&x->x_obj, 0);
- x->m.col = x->m.row = x->m2.col = x->m2.row = 0;
- x->m.atombuffer = x->m2.atombuffer = 0;
- return(x);
- }
- }
-
- static void mtx_sub_setup(void)
- {
- mtx_sub_class = class_new(gensym("mtx_sub"), (t_newmethod)mtx_sub_new, (t_method)mtx_binmtx_free,
- sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
- class_addcreator((t_newmethod)mtx_sub_new, gensym("mtx_-"), A_GIMME,0);
- class_addmethod(mtx_sub_class, (t_method)mtx_sub_matrix, gensym("matrix"), A_GIMME, 0);
- class_addmethod(mtx_sub_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
- class_addfloat (mtx_sub_class, mtx_sub_float);
- class_addbang (mtx_sub_class, mtx_binmtx_bang);
-
- mtx_subscalar_class = class_new(gensym("mtx_sub"), 0, (t_method)mtx_binscalar_free,
- sizeof(t_mtx_binscalar), 0, 0);
- class_addcreator(0, gensym("mtx_-"), 0, 0);
- class_addmethod(mtx_subscalar_class, (t_method)mtx_subscalar_matrix, gensym("matrix"), A_GIMME, 0);
- class_addlist (mtx_subscalar_class, mtx_subscalar_list);
- class_addbang (mtx_subscalar_class, mtx_binscalar_bang);
-
- class_sethelpsymbol(mtx_sub_class, gensym("iemmatrix/mtx_binops"));
- class_sethelpsymbol(mtx_subscalar_class, gensym("iemmatrix/mtx_binops"));
- }
-
-
- /* mtx_mul */
- static t_class *mtx_mul_class, *mtx_mulelement_class, *mtx_mulscalar_class;
-
- static void mtx_mul_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
- {
- t_matrix *m=&x->m, *m2=&x->m2;
- t_atom *ap, *ap1=argv+2, *ap2=m2->atombuffer+2;
- int row=atom_getfloat(argv), col=atom_getfloat(argv+1);
- int row2, col2, n, r, c;
-
- if (!m2->atombuffer){ post("mulitply with what ?"); return; }
- if (argc<2){ post("mtx_mul: crippled matrix"); return; }
- if ((col<1)||(row<1)){post("mtx_mul: invalid dimensions"); return; }
- if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
-
- row2=atom_getfloat(m2->atombuffer);
- col2=atom_getfloat(m2->atombuffer+1);
-
- if (col!=row2) { post("mtx_mul: matrix dimensions do not match !"); return; }
-
- adjustsize(m, row, col2);
- ap=m->atombuffer+2;
-
- for(r=0;r<row;r++)
- for(c=0;c<col2;c++) {
- t_matrixfloat sum = 0.f;
- for(n=0;n<col;n++)sum+=(t_matrixfloat)atom_getfloat(ap1+col*r+n)*atom_getfloat(ap2+col2*n+c);
- SETFLOAT(ap+col2*r+c,sum);
- }
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
- }
-
- static void mtx_mul_float(t_mtx_binmtx *x, t_float f)
- {
- t_matrix *m=&x->m, *m2=&x->m2;
- t_atom *ap, *ap2=m2->atombuffer+2;
- int row2, col2, n;
-
- if (!m2->atombuffer){ post("mulitply with what ?"); return; }
-
- row2=atom_getfloat(m2->atombuffer);
- col2=atom_getfloat(m2->atombuffer+1);
- adjustsize(m, row2, col2);
- ap=m->atombuffer+2;
-
- n=row2*col2;
-
- while(n--){
- SETFLOAT(ap, f*atom_getfloat(ap2++));
- ap++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
- }
-
- static void mtx_mulelement_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
- {
- int row=atom_getfloat(argv++);
- int col=atom_getfloat(argv++);
- t_atom *m;
- t_atom *m2 = x->m2.atombuffer+2;
- int n = argc-2;
-
- if (argc<2){ post("mtx_mul: crippled matrix"); return; }
- if ((col<1)||(row<1)) { post("mtx_mul: invalid dimensions"); return; }
- if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
- if (!(x->m2.col*x->m2.row)) {
- adjustsize(&x->m, row, col);
- matrix_set(&x->m, 0);
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- return;
- }
- if ((col!=x->m2.col)||(row!=x->m2.row)){ post("matrix dimension do not match"); /* LATER SOLVE THIS */ return; }
-
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- t_float f = atom_getfloat(argv++)*atom_getfloat(m2++);
- SETFLOAT(m, f);
- m++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
-
- static void mtx_mulscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc-2;
- t_atom *m;
- t_float factor = x->f;
- int row=atom_getfloat(argv++);
- int col=atom_getfloat(argv++);
-
- if (argc<2){
- post("mtx_mul: crippled matrix");
- return;
- }
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = atom_getfloat(argv++)*factor;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_mulscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc;
- t_atom *m;
- t_float factor = x->f;
- adjustsize(&x->m, 1, argc);
- m = x->m.atombuffer;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = atom_getfloat(argv++)*factor;
- }
- outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
- }
-
- static void *mtx_mul_new(t_symbol *s, int argc, t_atom *argv)
- {
- if (argc>1) post("mtx_mul : extra arguments ignored");
- if (argc) {
- t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_mulscalar_class);
- floatinlet_new(&x->x_obj, &x->f);
- x->f = atom_getfloatarg(0, argc, argv);
- outlet_new(&x->x_obj, 0);
- return(x);
- } else {
- if (s->s_name[4]=='.') {
- /* element mul */
-
- t_matrix *x = (t_matrix *)pd_new(mtx_mulelement_class);
- inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
- outlet_new(&x->x_obj, 0);
- x->col = x->row = 0;
- x->atombuffer = 0;
- return(x);
- } else {
- t_mtx_binmtx *x = (t_mtx_binmtx *)pd_new(mtx_mul_class);
- inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
- outlet_new(&x->x_obj, 0);
- x->m.col = x->m.row = x->m2.col = x->m2.row = 0;
- x->m.atombuffer = x->m2.atombuffer = 0;
- return (x);
- }
- }
- }
-
- static void mtx_mul_setup(void)
- {
- mtx_mul_class = class_new(gensym("mtx_mul"), (t_newmethod)mtx_mul_new, (t_method)mtx_binmtx_free,
- sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
- class_addcreator((t_newmethod)mtx_mul_new, gensym("mtx_*"), A_GIMME,0);
- class_addmethod(mtx_mul_class, (t_method)mtx_mul_matrix, gensym("matrix"), A_GIMME, 0);
- class_addmethod(mtx_mul_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
- class_addfloat (mtx_mul_class, mtx_mul_float);
- class_addbang (mtx_mul_class, mtx_binmtx_bang);
-
- mtx_mulelement_class = class_new(gensym("mtx_.*"), (t_newmethod)mtx_mul_new, (t_method)mtx_binmtx_free,
- sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
- class_addmethod(mtx_mulelement_class, (t_method)mtx_mulelement_matrix, gensym("matrix"), A_GIMME, 0);
- class_addmethod(mtx_mulelement_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
- class_addfloat (mtx_mulelement_class, mtx_mul_float);
- class_addbang (mtx_mulelement_class, mtx_binmtx_bang);
-
- mtx_mulscalar_class = class_new(gensym("mtx_mul"), 0, (t_method)mtx_binscalar_free,
- sizeof(t_mtx_binscalar), 0, 0);
- class_addcreator(0, gensym("mtx_*"), 0, 0);
- class_addcreator(0, gensym("mtx_.*"), 0, 0);
- class_addmethod(mtx_mulscalar_class, (t_method)mtx_mulscalar_matrix, gensym("matrix"), A_GIMME, 0);
- class_addlist (mtx_mulscalar_class, mtx_mulscalar_list);
- class_addbang (mtx_mulscalar_class, mtx_binscalar_bang);
-
- class_sethelpsymbol(mtx_mul_class, gensym("iemmatrix/mtx_binops"));
- class_sethelpsymbol(mtx_mulelement_class, gensym("iemmatrix/mtx_binops"));
- class_sethelpsymbol(mtx_mulscalar_class, gensym("iemmatrix/mtx_binops"));
- }
-
-
- /* mtx_div */
- static t_class *mtx_divelement_class, *mtx_divscalar_class;
-
- static void mtx_divelement_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
- {
- int row=atom_getfloat(argv++);
- int col=atom_getfloat(argv++);
- t_atom *m;
- t_atom *m2 = x->m2.atombuffer+2;
- int n = argc-2;
-
- if (argc<2){ post("mtx_div: crippled matrix"); return; }
- if ((col<1)||(row<1)) { post("mtx_div: invalid dimensions"); return; }
- if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
- if (!(x->m2.col*x->m2.row)) {
- adjustsize(&x->m, row, col);
- matrix_set(&x->m, 0);
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- return;
- }
- if ((col!=x->m2.col)||(row!=x->m2.row)){ post("matrix dimension do not match"); /* LATER SOLVE THIS */ return; }
-
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- t_float f = atom_getfloat(argv++)/atom_getfloat(m2++);
- SETFLOAT(m, f);
- m++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_divelement_float(t_mtx_binmtx *x, t_float f)
- {
- t_matrix *m=&x->m, *m2=&x->m2;
- t_atom *ap, *ap2=m2->atombuffer+2;
- int row2, col2, n;
-
- if (!m2->atombuffer){ post("divide by what ?"); return; }
-
- row2=atom_getfloat(m2->atombuffer);
- col2=atom_getfloat(m2->atombuffer+1);
- adjustsize(m, row2, col2);
- ap=m->atombuffer+2;
-
- n=row2*col2;
-
- while(n--){
- SETFLOAT(ap, f/atom_getfloat(ap2++));
- ap++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
- }
- static void mtx_divscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc-2;
- t_atom *m;
- t_float factor = 1.0/x->f;
- int row=atom_getfloat(argv++);
- int col=atom_getfloat(argv++);
-
- if (argc<2){
- post("mtx_div: crippled matrix");
- return;
- }
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = atom_getfloat(argv++)*factor;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_divscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc;
- t_atom *m;
- t_float factor = 1.0/x->f;
-
- adjustsize(&x->m, 1, argc);
- m = x->m.atombuffer;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = atom_getfloat(argv++)*factor;
- }
-
- outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
- }
-
- static void *mtx_div_new(t_symbol *s, int argc, t_atom *argv)
- {
- if (argc>1) post("mtx_div : extra arguments ignored");
- if (argc) {
- /* scalar division */
- t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_divscalar_class);
- floatinlet_new(&x->x_obj, &x->f);
- x->f = atom_getfloatarg(0, argc, argv);
- outlet_new(&x->x_obj, 0);
- return(x);
- } else {
- /* element division */
- t_matrix *x = (t_matrix *)pd_new(mtx_divelement_class);
- inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
- outlet_new(&x->x_obj, 0);
- x->col = x->row = 0;
- x->atombuffer = 0;
- return(x);
- }
- }
-
- static void mtx_div_setup(void)
- {
- mtx_divelement_class = class_new(gensym("mtx_./"), (t_newmethod)mtx_div_new, (t_method)mtx_binmtx_free,
- sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
- class_addmethod(mtx_divelement_class, (t_method)mtx_divelement_matrix, gensym("matrix"), A_GIMME, 0);
- class_addmethod(mtx_divelement_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
- class_addfloat (mtx_divelement_class, mtx_divelement_float);
- class_addbang (mtx_divelement_class, mtx_binmtx_bang);
-
- mtx_divscalar_class = class_new(gensym("mtx_./"), 0, (t_method)mtx_binscalar_free,
- sizeof(t_mtx_binscalar), 0, 0);
- class_addmethod(mtx_divscalar_class, (t_method)mtx_divscalar_matrix, gensym("matrix"), A_GIMME, 0);
- class_addlist (mtx_divscalar_class, mtx_divscalar_list);
- class_addbang (mtx_divscalar_class, mtx_binscalar_bang);
-
- class_sethelpsymbol(mtx_divelement_class, gensym("iemmatrix/mtx_binops"));
- class_sethelpsymbol(mtx_divscalar_class, gensym("iemmatrix/mtx_binops"));
- }
-
- /* mtx_pow */
- #ifdef __APPLE__
- /* there is no such thing like powf on apple... */
- static inline float powf (float v, float p)
- {
- return (float) pow((double)v, (double) p);
- }
- #endif /* __APPLE__ */
-
- static t_class *mtx_powelement_class, *mtx_powscalar_class;
-
- static void mtx_powelement_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
- {
- int row=atom_getfloat(argv++);
- int col=atom_getfloat(argv++);
- t_atom *m;
- t_atom *m2 = x->m2.atombuffer+2;
- int n = argc-2;
-
- if (argc<2){ post("mtx_pow: crippled matrix"); return; }
- if ((col<1)||(row<1)) { post("mtx_pow: invalid dimensions"); return; }
- if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
- if (!(x->m2.col*x->m2.row)) {
- adjustsize(&x->m, row, col);
- matrix_set(&x->m, 0);
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- return;
- }
- if ((col!=x->m2.col)||(row!=x->m2.row)){ post("matrix dimension do not match"); /* LATER SOLVE THIS */ return; }
-
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- t_float f = powf(atom_getfloat(argv++),atom_getfloat(m2++));
- SETFLOAT(m, f);
- m++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_powelement_float(t_mtx_binmtx *x, t_float f)
- {
- t_matrix *m=&x->m, *m2=&x->m2;
- t_atom *ap, *ap2=m2->atombuffer+2;
- int row2, col2, n;
-
- if (!m2->atombuffer){ post("power by what ?"); return; }
-
- row2=atom_getfloat(m2->atombuffer);
- col2=atom_getfloat(m2->atombuffer+1);
- adjustsize(m, row2, col2);
- ap=m->atombuffer+2;
-
- n=row2*col2;
-
- while(n--){
- SETFLOAT(ap, powf(f,atom_getfloat(ap2++)));
- ap++;
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
- }
- static void mtx_powscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc-2;
- t_atom *m;
- t_float factor = x->f;
- int row=atom_getfloat(argv++);
- int col=atom_getfloat(argv++);
-
- if (argc<2){
- post("mtx_pow: crippled matrix");
- return;
- }
- adjustsize(&x->m, row, col);
- m = x->m.atombuffer+2;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = powf(atom_getfloat(argv++),factor);
- }
-
- outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
- }
- static void mtx_powscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
- {
- int n=argc;
- t_atom *m;
- t_float factor = x->f;
-
- adjustsize(&x->m, 1, argc);
- m = x->m.atombuffer;
-
- while(n--){
- m->a_type = A_FLOAT;
- (m++)->a_w.w_float = powf(atom_getfloat(argv++),factor);
- }
-
- outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
- }
-
- static void *mtx_pow_new(t_symbol *s, int argc, t_atom *argv)
- {
- if (argc>1) post("mtx_pow : extra arguments ignored");
- if (argc) {
- /* scalar powision */
- t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_powscalar_class);
- floatinlet_new(&x->x_obj, &x->f);
- x->f = atom_getfloatarg(0, argc, argv);
- outlet_new(&x->x_obj, 0);
- return(x);
- } else {
- /* element powision */
- t_matrix *x = (t_matrix *)pd_new(mtx_powelement_class);
- inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
- outlet_new(&x->x_obj, 0);
- x->col = x->row = 0;
- x->atombuffer = 0;
- return(x);
- }
- }
-
- static void mtx_pow_setup(void)
- {
- mtx_powelement_class = class_new(gensym("mtx_.^"), (t_newmethod)mtx_pow_new, (t_method)mtx_binmtx_free,
- sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
- class_addmethod(mtx_powelement_class, (t_method)mtx_powelement_matrix, gensym("matrix"), A_GIMME, 0);
- class_addmethod(mtx_powelement_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
- class_addfloat (mtx_powelement_class, mtx_powelement_float);
- class_addbang (mtx_powelement_class, mtx_binmtx_bang);
-
- mtx_powscalar_class = class_new(gensym("mtx_.^"), 0, (t_method)mtx_binscalar_free,
- sizeof(t_mtx_binscalar), 0, 0);
- class_addmethod(mtx_powscalar_class, (t_method)mtx_powscalar_matrix, gensym("matrix"), A_GIMME, 0);
- class_addlist (mtx_powscalar_class, mtx_powscalar_list);
- class_addbang (mtx_powscalar_class, mtx_binscalar_bang);
-
- class_sethelpsymbol(mtx_powelement_class, gensym("iemmatrix/mtx_binops"));
- class_sethelpsymbol(mtx_powscalar_class, gensym("iemmatrix/mtx_binops"));
- }
-
- void mtx_binops_setup(void)
- {
- mtx_add_setup();
- mtx_sub_setup();
- mtx_mul_setup();
- mtx_div_setup();
- mtx_pow_setup();
-
- }
- void iemtx_binops_setup(void)
- {
- mtx_binops_setup();
- }
--- 54,55 ----
--- NEW FILE: mtx_add.c ---
/*
* iemmatrix
*
* objects for manipulating simple matrices
* mostly refering to matlab/octave matrix functions
*
* Copyright (c) IOhannes m zmölnig, forum::für::umläute
* IEM, Graz, Austria
*
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*
*/
#include "iemmatrix.h"
/*
mtx_add
mtx_+
*/
/* mtx_add */
static t_class *mtx_add_class, *mtx_addscalar_class;
static void mtx_addscalar_matrix(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc-2;
int row=atom_getfloat(argv), col=atom_getfloat(argv+1);
t_float offset=x->f;
t_atom *buf;
t_atom *ap=argv+2;
if(argc<2){post("mtx_add: crippled matrix");return; }
adjustsize(&x->m, row, col);
buf=x->m.atombuffer+2;
while(n--){
buf->a_type = A_FLOAT;
buf++->a_w.w_float = atom_getfloat(ap++) + offset;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_addscalar_list(t_mtx_binscalar *x, t_symbol *s, int argc, t_atom *argv)
{
int n=argc;
t_atom *m;
t_float offset = x->f;
adjustsize(&x->m, 1, argc);
m = x->m.atombuffer;
while(n--){
m->a_type = A_FLOAT;
(m++)->a_w.w_float = atom_getfloat(argv++) + offset;
}
outlet_list(x->x_obj.ob_outlet, gensym("list"), argc, x->m.atombuffer);
}
static void mtx_add_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv)
{
int row=atom_getfloat(argv);
int col=atom_getfloat(argv+1);
t_atom *m;
t_atom *m1 = argv+2;
t_atom *m2 = x->m2.atombuffer+2;
int n = argc-2;
if (argc<2){ post("mtx_add: crippled matrix"); return; }
if ((col<1)||(row<1)) { post("mtx_add: invalid dimensions"); return; }
if (col*row>argc-2){ post("sparse matrix not yet supported : use \"mtx_check\""); return; }
if (!(x->m2.col*x->m2.row)) {
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, argv);
return;
}
if ((col!=x->m2.col)||(row!=x->m2.row)){
post("mtx_add: matrix dimensions do not match");
/* LATER SOLVE THIS */
return;
}
adjustsize(&x->m, row, col);
m = x->m.atombuffer+2;
while(n--){
t_float f = atom_getfloat(m1++)+atom_getfloat(m2++);
SETFLOAT(m, f);
m++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer);
}
static void mtx_add_float(t_mtx_binmtx *x, t_float f)
{
t_matrix *m=&x->m, *m2=&x->m2;
t_atom *ap, *ap2=m2->atombuffer+2;
int row2, col2, n;
if (!m2->atombuffer){ post("mulitply with what ?"); return; }
row2=atom_getfloat(m2->atombuffer);
col2=atom_getfloat(m2->atombuffer+1);
adjustsize(m, row2, col2);
ap=m->atombuffer+2;
n=row2*col2;
while(n--){
SETFLOAT(ap, f+atom_getfloat(ap2++));
ap++;
}
outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), m->row*m->col+2, m->atombuffer);
}
static void *mtx_add_new(t_symbol *s, int argc, t_atom *argv)
{
if (argc>1) post("mtx_add : extra arguments ignored");
if (argc) {
t_mtx_binscalar *x = (t_mtx_binscalar *)pd_new(mtx_addscalar_class);
floatinlet_new(&x->x_obj, &x->f);
x->f = atom_getfloatarg(0, argc, argv);
outlet_new(&x->x_obj, 0);
return(x);
} else {
t_mtx_binmtx *x = (t_mtx_binmtx *)pd_new(mtx_add_class);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
outlet_new(&x->x_obj, 0);
x->m.col = x->m.row = x->m2.col = x->m2.row = 0;
x->m.atombuffer = x->m2.atombuffer = 0;
return(x);
}
}
void mtx_add_setup(void)
{
mtx_add_class = class_new(gensym("mtx_add"), (t_newmethod)mtx_add_new, (t_method)mtx_binmtx_free,
sizeof(t_mtx_binmtx), 0, A_GIMME, 0);
class_addcreator((t_newmethod)mtx_add_new, gensym("mtx_+"), A_GIMME,0);
class_addmethod(mtx_add_class, (t_method)mtx_add_matrix, gensym("matrix"), A_GIMME, 0);
class_addmethod(mtx_add_class, (t_method)mtx_bin_matrix2, gensym(""), A_GIMME, 0);
class_addfloat (mtx_add_class, mtx_add_float);
class_addbang (mtx_add_class, mtx_binmtx_bang);
mtx_addscalar_class = class_new(gensym("mtx_add"), 0, (t_method)mtx_binscalar_free,
sizeof(t_mtx_binscalar), 0, 0);
class_addcreator(0, gensym("mtx_+"), 0, 0);
class_addmethod(mtx_addscalar_class, (t_method)mtx_addscalar_matrix, gensym("matrix"), A_GIMME, 0);
class_addlist (mtx_addscalar_class, mtx_addscalar_list);
class_addbang (mtx_addscalar_class, mtx_binscalar_bang);
class_sethelpsymbol(mtx_add_class, gensym("iemmatrix/mtx_binops"));
class_sethelpsymbol(mtx_addscalar_class, gensym("iemmatrix/mtx_binops"));
}
void iemtx_add_setup(void)
{
mtx_add_setup();
}
Index: mtx_inverse.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_inverse.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** mtx_inverse.c 12 Apr 2005 09:01:36 -0000 1.2
--- mtx_inverse.c 11 May 2005 13:05:29 -0000 1.3
***************
*** 17,47 ****
static t_class *mtx_inverse_class;
- static void mtx_inverse_matrix(t_matrix *x, t_symbol *s, int argc, t_atom *argv)
- {
- /* maybe we should do this in double or long double ? */
- int row=atom_getfloat(argv);
- int col=atom_getfloat(argv+1);
- int i, k, row2=row*row;
! t_matrixfloat *original, *inverted;
! t_matrixfloat *a1, *a2, *b1, *b2; // dummy pointers
! int ok = 0;
! if(row*col+2>argc){
! post("mtx_print : sparse matrices not yet supported : use \"mtx_check\"");
! return;
! }
! if (row!=col){
! post("mtx_inverse: only square matrices can be inverted");
! return;
! }
! // reserve memory for outputting afterwards
! adjustsize(x, row, row);
! // 1. get the 2 matrices : orig; invert (create as eye, but will be orig^(-1))
! inverted = (t_matrixfloat *)getbytes(sizeof(t_matrixfloat)*row2);
! // 1a extract values of A to float-buf
! original=matrix2float(argv);
// 1b make an eye-shaped float-buf for B
i=row2;
--- 17,37 ----
static t_class *mtx_inverse_class;
! t_matrixfloat* mtx_doInvert(t_matrixfloat*input, int rowcol){
! /*
! * row==col==rowclo
! * input=t_matrixfloat[row*col]
! * output=t_matrixfloat[row*col]
! */
! int i, k;
! t_matrixfloat *a1, *b1, *a2, *b2;
! int ok=0; /* error counter */
! int col=rowcol, row=rowcol, row2=row*col;
! t_matrixfloat *original=input;
! // 1a reserve space for the inverted matrix
! t_matrixfloat *inverted = (t_matrixfloat *)getbytes(sizeof(t_matrixfloat)*row2);
// 1b make an eye-shaped float-buf for B
i=row2;
***************
*** 59,63 ****
if (!diagel)ok++;
-
/* normalize current row (set the diagonal-element to 1 */
a2=original+k*col;
--- 49,52 ----
***************
*** 74,94 ****
for(i=0;i<row;i++)
if (i-k) {
! t_matrixfloat f=-*(original+i*row+k);
! int j = row;
! a1=original+i*row;
! b1=inverted+i*row;
! while (j--) {
! *(a1+j)+=f**(a2+j);
! *(b1+j)+=f**(b2+j);
! }
}
}
// 3. output the matrix
// 3a convert the floatbuf to an atombuf;
float2matrix(x->atombuffer, inverted);
// 3b destroy the buffers
! freebytes(original, sizeof(t_matrixfloat)*row2);
!
! if (ok)post("mtx_inverse: couldn't really invert the matrix !!! %d error%c", ok, (ok-1)?'s':0);
// 3c output the atombuf;
--- 63,121 ----
for(i=0;i<row;i++)
if (i-k) {
! t_matrixfloat f=-*(original+i*row+k);
! int j = row;
! a1=original+i*row;
! b1=inverted+i*row;
! while (j--) {
! *(a1+j)+=f**(a2+j);
! *(b1+j)+=f**(b2+j);
! }
}
}
+
+ if (ok)post("mtx_inverse: couldn't really invert the matrix !!! %d error%c", ok, (ok-1)?'s':0);
+
+ return inverted;
+ }
+
+ static void mtx_inverse_matrix(t_matrix *x, t_symbol *s, int argc, t_atom *argv)
+ {
+ /* maybe we should do this in double or long double ? */
+ int row=atom_getfloat(argv);
+ int col=atom_getfloat(argv+1);
+
+ t_matrixfloat *original, *inverted;
+
+ if(row*col+2>argc){
+ post("mtx_print : sparse matrices not yet supported : use \"mtx_check\"");
+ return;
+ }
+
+ // 1 extract values of A to float-buf
+ original=matrix2float(argv);
+
+ // reserve memory for outputting afterwards
+ adjustsize(x, col, row);
+
+ if (row!=col){
+ // we'll have to do the pseudo-inverse:
+ // P=A'*inv(A*A');
+ t_matrixfloat*transposed=mtx_doTranspose(original, row, col);
+ t_matrixfloat*invertee =mtx_doMultiply(row, original, col, transposed, row);
+ inverted=mtx_doMultiply(col, transposed, row, mtx_doInvert(invertee, row), row);
+
+ freebytes(transposed, sizeof(t_matrixfloat)*col*row);
+ freebytes(invertee , sizeof(t_matrixfloat)*row*row);
+
+ } else {
+ inverted=mtx_doInvert(original, row);
+ }
+
// 3. output the matrix
// 3a convert the floatbuf to an atombuf;
float2matrix(x->atombuffer, inverted);
// 3b destroy the buffers
! freebytes(original, sizeof(t_matrixfloat)*row*col);
! freebytes(inverted, sizeof(t_matrixfloat)*row*row);
// 3c output the atombuf;
***************
*** 108,112 ****
{
mtx_inverse_class = class_new(gensym("mtx_inverse"), (t_newmethod)mtx_inverse_new,
! (t_method)matrix_free, sizeof(t_matrix), 0, A_GIMME, 0);
class_addbang (mtx_inverse_class, matrix_bang);
class_addmethod(mtx_inverse_class, (t_method)mtx_inverse_matrix, gensym("matrix"), A_GIMME, 0);
--- 135,139 ----
{
mtx_inverse_class = class_new(gensym("mtx_inverse"), (t_newmethod)mtx_inverse_new,
! (t_method)matrix_free, sizeof(t_matrix), 0, A_GIMME, 0);
class_addbang (mtx_inverse_class, matrix_bang);
class_addmethod(mtx_inverse_class, (t_method)mtx_inverse_matrix, gensym("matrix"), A_GIMME, 0);
Index: iemmatrix.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/iemmatrix.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** iemmatrix.c 10 May 2005 12:16:49 -0000 1.6
--- iemmatrix.c 11 May 2005 13:05:28 -0000 1.7
***************
*** 14,18 ****
#include "iemmatrix.h"
! void mtx_binops_setup();
void mtx_col_setup();
void mtx_cholesky_setup();
--- 14,22 ----
#include "iemmatrix.h"
! void mtx_mul_setup();
! void mtx_div_setup();
! void mtx_add_setup();
! void mtx_sub_setup();
! void mtx_pow_setup();
void mtx_col_setup();
void mtx_cholesky_setup();
***************
*** 45,49 ****
void iemtx_setup(){
! mtx_binops_setup();
mtx_col_setup();
mtx_cholesky_setup();
--- 49,57 ----
void iemtx_setup(){
! mtx_mul_setup();
! mtx_div_setup();
! mtx_add_setup();
! mtx_sub_setup();
! mtx_pow_setup();
mtx_col_setup();
mtx_cholesky_setup();
Index: iemmatrix.h
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/iemmatrix.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** iemmatrix.h 10 May 2005 09:41:16 -0000 1.5
--- iemmatrix.h 11 May 2005 13:05:28 -0000 1.6
***************
*** 135,137 ****
--- 135,147 ----
void mtx_binscalar_free(t_mtx_binscalar *x);
+
+ /* some math */
+
+ /* invert a square matrix (row=col=rowcol) */
+ t_matrixfloat*mtx_doInvert(t_matrixfloat*input, int rowcol);
+ /* transpose a matrix */
+ t_matrixfloat*mtx_doTranspose(t_matrixfloat*output, int row, int col);
+ /* multiply matrix A=[rowA*colA] with matrix B=[rowB*colB]; C=A*B; colA=rowB=colArowB */
+ t_matrixfloat*mtx_doMultiply(int rowA, t_matrixfloat*A, int colArowB, t_matrixfloat*B, int colB);
+
#endif