Re: where is Math.Matrix->`/ ?

Something like this, maybe?

Feel free to patch your Pike and test it out. I'm not checking this in
for the current 7.8.

/Mirar

 | > Math.Matrix m=Math.Matrix(3,4);
 | (2) Result: Math.Matrix( ({ ({      1,      0,      0}),
 |                             ({      0,      1,      0}),
 |                             ({      0,      0,      1}),
 |                             ({      0,      0,      0}) }) )
 | > for (int i=0; i<3; i++) for (int j=0; j<4; j++) m->poke(i,j,i*10+j); m;
 | Ok.
 | (3) Result: Math.Matrix( ({ ({      0,     10,     20}),
 |                             ({      1,     11,     21}),
 |                             ({      2,     12,     22}),
 |                             ({      3,     13,     23}) }) )
 | > m->peek(1,1);                                                          
 | (4) Result: 11.0
 | > m->peek(2,1);
 | (5) Result: 21.0

 | > object m=Math.Matrix(({1,2,3,4})/2);
 | > object m2=Math.Matrix(m);
 | > m2->poke(0,1,17);
 | (1) Result: Math.Matrix( ({ ({      1,      2}),
 |                             ({     17,      4}) }) )
 | > m;
 | (2) Result: Math.Matrix( ({ ({      1,      2}),
 |                             ({      3,      4}) }) )

Patch:

Index: math_matrix.c
===================================================================
RCS file: /pike/data/cvsroot/Pike/7.8/src/modules/Math/math_matrix.c,v
retrieving revision 1.40
diff -u -r1.40 math_matrix.c
--- math_matrix.c	18 Dec 2007 23:24:49 -0000	1.40
+++ math_matrix.c	24 Aug 2008 16:13:40 -0000
@@ -64,7 +64,9 @@
 #define Xmatrix(X) PIKE_CONCAT(X,imatrix)
 #define XmatrixY(X,Y) PIKE_CONCAT3(X,imatrix,Y)
 #define PUSH_ELEM( X )  push_int( (INT_TYPE)(X) )
+#define INT_ELEMS
 #include <matrix_code.h>
+#undef INT_ELEMS
 #undef PUSH_ELEM
 #undef Xmatrix
 #undef matrixX
@@ -81,7 +83,9 @@
 #define Xmatrix(X) PIKE_CONCAT(X,lmatrix)
 #define XmatrixY(X,Y) PIKE_CONCAT3(X,lmatrix,Y)
 #define PUSH_ELEM( X )  push_int64( (INT64)(X) )
+#define INT_ELEMS
 #include <matrix_code.h>
+#undef INT_ELEMS
 #undef PUSH_ELEM
 #undef Xmatrix
 #undef matrixX
@@ -114,7 +118,9 @@
 #define Xmatrix(X) PIKE_CONCAT(X,smatrix)
 #define XmatrixY(X,Y) PIKE_CONCAT3(X,smatrix,Y)
 #define PUSH_ELEM( X )  push_int( (INT_TYPE)(X) )
+#define INT_ELEMS
 #include <matrix_code.h>
+#undef INT_ELEMS
 #undef PUSH_ELEM
 #undef Xmatrix
 #undef matrixX
@@ -150,6 +156,10 @@
  *! rotation matrix.
  */

+/*! @decl void create(Matrix to_clone)
+ *! When given another @[Matrix] of similar type as argumnet, this is cloned.
+ */
+
 /* ---------------------------------------------------------------- */

 /*! @decl array(array) cast(string to_what)
@@ -237,6 +247,17 @@
  *!     Returns the height of the matrix.
  */

+/*! @decl Matrix poke(int x,int y,float value)
+ *! @decl Matrix poke(int column,int row,float value)
+ *!     Destructively change a value in the matrix. 0,0 is top left.
+ */
+
+/*! @decl float peek(int x,int y)
+ *! @decl float peek(int column,int row)
+ *!     Read out one value from the matrix. 0,0 is top left.
+ */
+
+
 /*! @endclass
  */

Index: matrix_code.h
===================================================================
RCS file: /pike/data/cvsroot/Pike/7.8/src/modules/Math/matrix_code.h,v
retrieving revision 1.20
diff -u -r1.20 matrix_code.h
--- matrix_code.h	28 Jun 2008 23:05:59 -0000	1.20
+++ matrix_code.h	24 Aug 2008 16:13:40 -0000
@@ -204,6 +204,26 @@
 done_made:
       ;
    }
+   else if (Pike_sp[-args].type==T_OBJECT)
+   {
+      struct matrixX(_storage) *mx=NULL;
+
+      if (Pike_sp[-args].type!=T_OBJECT ||
+	  !((mx=(struct matrixX(_storage)*)
+	     get_storage(Pike_sp[-1].u.object,XmatrixY(math_,_program)))))
+	 SIMPLE_BAD_ARG_ERROR(PNAME,1,"object(Math."PNAME")");
+      
+      THIS->xsize=xs=mx->xsize;
+      THIS->ysize=ys=mx->ysize;
+
+      THIS->m=m=malloc(sizeof(FTYPE)*xs*ys);
+      if (!m)
+	 SIMPLE_OUT_OF_MEMORY_ERROR(PNAME,
+				    sizeof(FTYPE)*xs*ys);
+      MEMCPY(THIS->m,
+	     mx->m,
+	     sizeof(FTYPE)*xs*ys);
+   }
    else if (Pike_sp[-args].type==T_STRING)
    {
       char *dummy;
@@ -939,6 +959,42 @@
 }

 
+static void matrixX(_poke)(INT32 args)
+{
+   INT_TYPE row,column;
+#ifdef INT_ELEMS
+   LONGEST value;
+   get_all_args("poke",args,"%i%i%l",&column,&row,&value);
+#else
+   FLOAT_TYPE value;
+   get_all_args("poke",args,"%i%i%F",&column,&row,&value);
+#endif
+
+   if (row<0 || row>=THIS->ysize)
+      SIMPLE_BAD_ARG_ERROR("poke",2, "No such row");
+   if (column<0 || column>=THIS->ysize)
+      SIMPLE_BAD_ARG_ERROR("poke",2, "No such column");
+
+   THIS->m[row*THIS->xsize+column]=(FTYPE)value;
+
+   pop_n_elems(args);
+   ref_push_object(THISOBJ);
+}
+
+static void matrixX(_peek)(INT32 args)
+{
+   INT_TYPE row,column;
+
+   get_all_args("poke",args,"%i%i",&column,&row);
+   if (row<0 || row>=THIS->ysize)
+      SIMPLE_BAD_ARG_ERROR("peek",2, "No such row");
+   if (column<0 || column>=THIS->ysize)
+      SIMPLE_BAD_ARG_ERROR("peek",2, "No such column");
+
+   pop_n_elems(args);
+   PUSH_ELEM(THIS->m[row*THIS->xsize+column]);
+}
+
 /* ---------------------------------------------------------------- */

 void Xmatrix(init_math_)(void)
@@ -959,9 +1015,10 @@
    set_exit_callback(Xmatrix(exit_));

    ADD_FUNCTION("create",matrixX(_create),
-		tOr4( tFunc(tArr(tArr(tOr(tInt,tFloat))), tVoid),
+		tOr5( tFunc(tArr(tArr(tOr(tInt,tFloat))), tVoid),
 		      tFunc(tArr(tOr(tInt,tFloat)), tVoid),
 		      tFuncV(tStr, tMix, tVoid),
+		      tFunc(tObj, tVoid),
 		      tFunc(tInt1Plus tInt1Plus tOr4(tInt,tFloat,tString,tVoid), tVoid)), ID_PROTECTED);

    ADD_FUNCTION("cast",matrixX(_cast),
@@ -1008,6 +1065,9 @@
    ADD_FUNCTION("xsize", matrixX(_xsize), tFunc(tNone, tInt), 0);
    ADD_FUNCTION("ysize", matrixX(_ysize), tFunc(tNone, tInt), 0);

+   ADD_FUNCTION("poke", matrixX(_poke), tFunc(tInt tInt tOr(tFloat,tInt),tObj), 0);
+   ADD_FUNCTION("peek", matrixX(_peek), tFunc(tInt tInt,PTYPE), 0);
+
    Pike_compiler->new_program->flags |= 
      PROGRAM_CONSTANT |
      PROGRAM_NO_EXPLICIT_DESTRUCT ;


Gmane