"extend matrix multiply to support vector inputs"
Brought to you by:
johnston
|
From: <ivt...@li...> - 2002-04-12 17:48:44
|
Patch: ivtools-020412-johnston-046
For: ivtools-1.0.3
Author: joh...@us...
Subject: extend matrix multiply to support vector inputs
Requires:
This is an intermediate patch to ivtools-1.0.3. To apply, cd to the
top-level directory of the ivtools source tree (the directory with src
and config subdirs), and apply like this:
patch -p0 <ThisFile
Summary of Changes:
- extend matrix multiply to support vector inputs. Now:
a=list,list(1),list(2),list(3)
b=list,list(1,2,3)
a*b
Yields the same as:
a=1,2,3
b=list,list(1,2,3)
a*b
Conversely:
a=list,list(1,2,3)
b=list,list(1),list(2),list(3)
a*b
Yields the same as:
a=list,list(1,2,3)
b=1,2,3
a*b
And:
list(2)*list(3)
Yields:
{{6}}
Index: ComTerp/numfunc.c
diff -c ComTerp/numfunc.c:1.4 ComTerp/numfunc.c:1.5
*** ComTerp/numfunc.c:1.4 Thu Apr 11 10:48:11 2002
--- src/ComTerp/numfunc.c Fri Apr 12 10:37:25 2002
***************
*** 27,32 ****
--- 27,33 ----
#include <ComTerp/comterp.h>
#include <Unidraw/iterator.h>
#include <Attribute/attrlist.h>
+ #include <OS/math.h>
#include <math.h>
#define TITLE "NumFunc"
***************
*** 455,461 ****
list1->GetAttrVal(it2)->array_val()->Number() : 0;
/* ensure inner dimension is the same */
! if (j1max != i2max) return nil;
/* ensure each row is of equal length */
list1->First(it1);
--- 456,463 ----
list1->GetAttrVal(it2)->array_val()->Number() : 0;
/* ensure inner dimension is the same */
! /* allow for vector argument on rhs */
! if (j1max != i2max && (j1max || i2max!=1)) return nil;
/* ensure each row is of equal length */
list1->First(it1);
***************
*** 494,533 ****
AttributeValueList* row1 = row1v && row1v->is_array() ?
row1v->array_val() : nil;
! if (!row1) break;
!
! /* loop over output columns */
! for (int j3=0; j3<j3max; j3++) {
! row1->First(itj1);
/* generate inner product */
! for (int n=0; n<j1max; n++) {
!
! /* locate the value from the second matrix */
! Iterator iti2, itj2;
list2->First(iti2);
for (int i=0; i<n; i++) list2->Next(iti2);
! AttributeValue* row2v = list1->GetAttrVal(iti2);
! AttributeValueList* row2 = row2v && row2v->is_array() ?
! row2v->array_val() : nil;
! if (row2) {
! row2->First(itj2);
! for (int j=0; j<j3; j++) row2->Next(itj2);
! }
! if (row1 && row2) {
! comterp()->push_stack(*row1->GetAttrVal(itj1));
! comterp()->push_stack(*row2->GetAttrVal(itj2));
exec(2,0);
if (n) addfunc.exec(2,0);
}
!
! row1->Next(itj1);
}
prodrow->Append(new AttributeValue(comterp()->pop_stack()));
}
- /* done looping over output columsn */
list1->Next(iti1);
}
--- 496,566 ----
AttributeValueList* row1 = row1v && row1v->is_array() ?
row1v->array_val() : nil;
! if (j3max) {
! /* loop over output columns */
! for (int j3=0; j3<j3max; j3++) {
!
! if (row1) row1->First(itj1);
!
! /* generate inner product */
! for (int n=0; n<Math::max(j1max,1); n++) {
!
! /* locate the value from the second matrix */
! Iterator iti2, itj2;
! list2->First(iti2);
! for (int i=0; i<n; i++) list2->Next(iti2);
! AttributeValue* row2v = list1->GetAttrVal(iti2);
! AttributeValueList* row2 = row2v && row2v->is_array() ?
! row2v->array_val() : nil;
! if (row2) {
! row2->First(itj2);
! for (int j=0; j<j3; j++) row2->Next(itj2);
! }
! if ((row1 || !j1max) && row2) {
! if (row1)
! comterp()->push_stack(*row1->GetAttrVal(itj1));
! else
! comterp()->push_stack(*row1v);
! comterp()->push_stack(*row2->GetAttrVal(itj2));
! exec(2,0);
! if (n) addfunc.exec(2,0);
! }
!
! if (row1) row1->Next(itj1);
! }
!
! prodrow->Append(new AttributeValue(comterp()->pop_stack()));
! }
! /* done looping over output columsn */
! } else {
! /* handle single output column */
+ if (row1) row1->First(itj1);
+
/* generate inner product */
! for (int n=0; n<Math::max(j1max,1); n++) {
!
! /* locate the value from the lhs vector */
! Iterator iti2;
list2->First(iti2);
for (int i=0; i<n; i++) list2->Next(iti2);
! AttributeValue* val2v = list1->GetAttrVal(iti2);
! if ((row1 || !j1max) && val2v) {
! if (row1)
! comterp()->push_stack(*row1->GetAttrVal(itj1));
! else
! comterp()->push_stack(*row1v);
! comterp()->push_stack(*val2v);
exec(2,0);
if (n) addfunc.exec(2,0);
}
!
! if (row1) row1->Next(itj1);
}
prodrow->Append(new AttributeValue(comterp()->pop_stack()));
}
list1->Next(iti1);
}
*** /dev/null Fri Apr 12 10:37:50 PDT 2002
--- patches/ivtools-020412-johnston-046
*************** patches/ivtools-020412-johnston-046
*** 0 ****
--- 1 ----
+ ivtools-020412-johnston-046
|