11package symjava .math ;
22
3+ import static com .sun .org .apache .bcel .internal .generic .InstructionConstants .DADD ;
4+ import static com .sun .org .apache .bcel .internal .generic .InstructionConstants .FADD ;
5+ import static com .sun .org .apache .bcel .internal .generic .InstructionConstants .IADD ;
6+ import static com .sun .org .apache .bcel .internal .generic .InstructionConstants .LADD ;
7+
38import java .util .ArrayList ;
49import java .util .List ;
10+ import java .util .Map ;
11+
12+ import com .sun .org .apache .bcel .internal .Constants ;
13+ import com .sun .org .apache .bcel .internal .generic .ConstantPoolGen ;
14+ import com .sun .org .apache .bcel .internal .generic .InstructionFactory ;
15+ import com .sun .org .apache .bcel .internal .generic .InstructionHandle ;
16+ import com .sun .org .apache .bcel .internal .generic .InstructionList ;
17+ import com .sun .org .apache .bcel .internal .generic .MethodGen ;
18+ import com .sun .org .apache .bcel .internal .generic .ObjectType ;
19+ import com .sun .org .apache .bcel .internal .generic .Type ;
520
621import symjava .matrix .SymVector ;
722import symjava .symbolic .Add ;
823import symjava .symbolic .Expr ;
924import symjava .symbolic .SymReal ;
1025import symjava .symbolic .TypeInfo ;
26+ import symjava .symbolic .Vector ;
27+ import symjava .symbolic .Expr .TYPE ;
28+ import symjava .symbolic .arity .BinaryOp ;
29+ import symjava .symbolic .utils .BytecodeUtils ;
1130import symjava .symbolic .utils .Utils ;
1231
1332/**
1433 * Dot Product of two vectors
1534 *
1635 */
17- public class Dot extends Expr {
18- protected SymVector left ;
19- protected SymVector right ;
36+ public class Dot extends BinaryOp {
2037 protected Expr expr = null ;
2138 public Dot (SymVector l , SymVector r ) {
39+ super (l ,r );
2240 if (l .dim () != r .dim ())
2341 throw new IllegalArgumentException ("The size of the two vector must be the same!" );
24- left = l ;
25- right = r ;
26- if (left instanceof Grad && right instanceof Grad ) {
27- label = left + " \\ cdot " + right ;
42+ arg1 = l ;
43+ arg2 = r ;
44+ if (arg1 instanceof Grad && arg2 instanceof Grad ) {
45+ label = arg1 + " \\ cdot " + arg2 ;
2846 sortKey = label ;
2947 return ;
3048 }
3149 List <Expr > list = new ArrayList <Expr >();
32- for (int i =0 ; i <left .dim (); i ++) {
33- list .add (left .get (i ).multiply (right .get (i )));
50+ for (int i =0 ; i <l .dim (); i ++) {
51+ list .add (l .get (i ).multiply (r .get (i )));
3452 }
3553 expr = Utils .addListToExpr (list ).simplify ();
3654 label = expr .toString ();
3755 sortKey = label ;
3856 }
3957
58+ public Dot (Vector l , Vector r ) {
59+ super (l ,r );
60+ if (l .dim () != r .dim ())
61+ throw new IllegalArgumentException ("The size of the two vector must be the same!" );
62+ arg1 = l ;
63+ arg2 = r ;
64+ label = "dot(" + arg1 + ", " + arg2 + ")" ;
65+ sortKey = label ;
66+ }
67+
4068 public static Expr apply (SymVector l , SymVector r ) {
4169 List <Expr > list = new ArrayList <Expr >();
4270 for (int i =0 ; i <l .dim (); i ++) {
@@ -51,20 +79,24 @@ public static Expr apply(SymVector l, SymVector r) {
5179 return dot ;
5280 }
5381
82+ public static Expr apply (Vector l , Vector r ) {
83+ return new Dot (l , r );
84+ }
85+
5486 @ Override
5587 public Expr diff (Expr expr ) {
5688 if (this .expr == null ) {
57- Grad lg = (Grad )left ;
58- Grad rg = (Grad )right ;
89+ Grad lg = (Grad )arg1 ;
90+ Grad rg = (Grad )arg2 ;
5991 if (lg .isAbstract () && rg .isAbstract ()) {
6092 Expr d1 = Dot .apply (new Grad (lg .getFunc ().diff (expr ), lg .getFunc ().args ), rg );
6193 Expr d2 = Dot .apply (lg , new Grad (rg .getFunc ().diff (expr ), rg .getFunc ().args ));
6294 return Add .simplifiedIns (d1 , d2 );
6395 }
6496 }
65- if (left instanceof Grad && right instanceof Grad ) {
66- Grad lg = (Grad )left ;
67- Grad rg = (Grad )right ;
97+ if (arg1 instanceof Grad && arg2 instanceof Grad ) {
98+ Grad lg = (Grad )arg1 ;
99+ Grad rg = (Grad )arg2 ;
68100 Expr d1 = Dot .apply (lg .diff (expr ), rg );
69101 Expr d2 = Dot .apply (lg , rg .diff (expr ));
70102 return Add .simplifiedIns (d1 , d2 );
@@ -75,11 +107,11 @@ public Expr diff(Expr expr) {
75107 @ Override
76108 public Expr fdiff (Expr f , Expr df ) {
77109 if (expr == null ) {
78- Grad lg = (Grad )left ;
79- Grad rg = (Grad )right ;
110+ Grad lg = (Grad )arg1 ;
111+ Grad rg = (Grad )arg2 ;
80112 if (lg .isAbstract () && rg .isAbstract ()) {
81113 Expr d1 = Dot .apply (new Grad (lg .getFunc ().fdiff (f , df )), rg );
82- Expr d2 = Dot .apply (lg , new Grad (rg .getFunc ().fdiff (f , df )));
114+ Expr d2 = Dot .apply (lg , new Grad (rg .getFunc ().fdiff (f , df )));
83115 return Add .shallowSimplifiedIns (d1 , d2 );
84116 }
85117 }
@@ -125,7 +157,7 @@ public Expr subs(Expr from, Expr to) {
125157 if (Utils .symCompare (this , from ))
126158 return to ;
127159 if (expr == null )
128- return new Dot (left .subs (from , to ), right .subs (from , to ));
160+ return new Dot (( SymVector ) arg1 .subs (from , to ), ( SymVector ) arg2 .subs (from , to ));
129161 else
130162 return expr .subs (from , to );
131163 }
@@ -135,25 +167,36 @@ public Expr getExpr() {
135167 return this .expr ;
136168 else {
137169 List <Expr > list = new ArrayList <Expr >();
138- for (int i =0 ; i <left .dim (); i ++) {
139- list .add (left .get (i ).multiply (right .get (i )));
170+ for (int i =0 ; i <arg1 .dim (); i ++) {
171+ list .add (arg1 .get (i ).multiply (arg2 .get (i )));
140172 }
141173 return Utils .addListToExpr (list ).simplify ();
142174 }
143175 }
144-
145- @ Override
146- public Expr [] args () {
147- // TODO Auto-generated method stub
148- return null ;
149- }
150-
176+
151177 @ Override
152- public TypeInfo getTypeInfo () {
153- // TODO Auto-generated method stub
154- return null ;
178+ public InstructionHandle bytecodeGen (String clsName , MethodGen mg ,
179+ ConstantPoolGen cp , InstructionFactory factory ,
180+ InstructionList il , Map <String , Integer > argsMap , int argsStartPos ,
181+ Map <Expr , Integer > funcRefsMap ) {
182+ InstructionHandle startPos = arg1 .bytecodeGen (clsName , mg , cp , factory , il , argsMap , argsStartPos , funcRefsMap );
183+ if (arg1 .getType () == TYPE .VECTOR && arg2 .getType () == TYPE .VECTOR ) {
184+ arg2 .bytecodeGen (clsName , mg , cp , factory , il , argsMap , argsStartPos , funcRefsMap );
185+ il .append (factory .createInvoke ("symjava.symbolic.utils.BytecodeOpSupport" , "dot" ,
186+ //Type.DOUBLE, new Type[] { new ObjectType("Jama.Matrix"),new ObjectType("Jama.Matrix") },
187+ new ObjectType ("Jama.Matrix" ), new Type [] { new ObjectType ("Jama.Matrix" ),new ObjectType ("Jama.Matrix" ) },
188+ Constants .INVOKESTATIC ));
189+ return startPos ;
190+ }
191+ //TODO
192+ return startPos ;
155193 }
156194
195+ // @Override
196+ // public TypeInfo getTypeInfo() {
197+ // return TypeInfo.tiDouble;
198+ // }
199+
157200 @ Override
158201 public void updateLabel () {
159202 // TODO Auto-generated method stub
0 commit comments