KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > vectorJep > function > VList


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 package org.lsmp.djep.vectorJep.function;
11
12 import java.util.*;
13 import org.nfunk.jep.*;
14 import org.lsmp.djep.vectorJep.Dimensions;
15 import org.lsmp.djep.vectorJep.values.*;
16
17 /**
18  * A enhanced version of List, allows matricies and tensors.
19  *
20  * @author Rich Morris
21  * Created on 27-Nov-2003
22  */

23 public class VList extends org.nfunk.jep.function.List
24     implements NaryOperatorI
25 {
26     public VList()
27     {
28         numberOfParameters = -1;
29     }
30
31     /** Calculates the dimension of this node with given dimesions of children. */
32     public Dimensions calcDim(Dimensions dims[]) throws ParseException
33     {
34         return Dimensions.valueOf(dims.length,dims[0]);
35     }
36     
37     /** Calculates the value of this node.
38      *
39      */

40     public MatrixValueI calcValue(MatrixValueI res,
41         MatrixValueI inputs[]) throws ParseException
42     {
43         int eleSize = inputs[0].getNumEles();
44         for(int i=0;i<inputs.length;++i)
45         {
46             for(int j=0;j<eleSize;++j)
47             {
48                 res.setEle(i*eleSize+j,inputs[i].getEle(j));
49             }
50         }
51         return res;
52     }
53     
54     public void run(Stack inStack) throws ParseException
55     {
56         checkStack(inStack); // check the stack
57
if(curNumberOfParameters <1)
58             throw new ParseException("Empty list");
59         Object JavaDoc param1 = inStack.pop();
60         
61         if(param1 instanceof Vector)
62         {
63             Vector vec1 = (Vector) param1;
64             int rows = curNumberOfParameters;
65             int cols = vec1.size();
66             Matrix res = new Matrix(rows,cols);
67             for(int j=0;j<cols;++j)
68                 res.setEle(rows-1,j,vec1.elementAt(j));
69             for(int i=rows-2;i>=0;--i)
70             {
71                 Vector vec = (Vector) inStack.pop();
72                 for(int j=0;j<cols;++j)
73                     res.setEle(i,j,vec.elementAt(j));
74             }
75             inStack.push(res);
76             return;
77         }
78         else if(param1 instanceof MatrixValueI)
79         {
80             MatrixValueI mat1 = (MatrixValueI) param1;
81             int rows = curNumberOfParameters;
82             int neles = mat1.getNumEles();
83             MatrixValueI res = Tensor.getInstance(Dimensions.valueOf(rows,mat1.getDim()));
84             for(int j=0;j<neles;++j)
85                 res.setEle((rows-1)*neles+j,mat1.getEle(j));
86             for(int i=rows-2;i>=0;--i)
87             {
88                 MatrixValueI mat = (MatrixValueI) inStack.pop();
89                 for(int j=0;j<neles;++j)
90                     res.setEle(i*neles+j,mat.getEle(j));
91             }
92             inStack.push(res);
93             return;
94         }
95         else
96         {
97             MVector res = new MVector(curNumberOfParameters);
98             res.setEle(curNumberOfParameters-1,param1);
99             for(int i=curNumberOfParameters-2;i>=0;--i)
100             {
101                 Object JavaDoc param = inStack.pop();
102                 res.setEle(i,param);
103             }
104             inStack.push(res);
105             return;
106         }
107     }
108 }
109
Popular Tags