KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > physics > relativity > Rank1Tensor


1 package JSci.physics.relativity;
2
3 import JSci.GlobalSettings;
4 import JSci.maths.*;
5 import JSci.maths.vectors.Double3Vector;
6
7 /**
8 * The Rank1Tensor class encapsulates 1st rank tensors.
9 * @version 1.0
10 * @author Mark Hale
11 */

12 public class Rank1Tensor extends Tensor {
13         protected double rank1[];
14
15         /**
16         * Constructs a 1st rank tensor.
17         */

18         public Rank1Tensor() {
19                 rank1=new double[4];
20         }
21         /**
22         * Constructs a 1st rank tensor.
23         * @param s a scalar
24         * @param v a 3-vector
25         */

26         public Rank1Tensor(double s,Double3Vector v) {
27                 this();
28                 rank1[0]=s;
29                 rank1[1]=v.getComponent(0);
30                 rank1[2]=v.getComponent(1);
31                 rank1[3]=v.getComponent(2);
32         }
33         /**
34         * Constructs a 1st rank tensor.
35         * @param s a scalar
36         * @param v1 1st 3-vector component
37         * @param v2 2nd 3-vector component
38         * @param v3 3rd 3-vector component
39         */

40         public Rank1Tensor(double s, double v1, double v2, double v3) {
41                 this();
42                 rank1[0]=s;
43                 rank1[1]=v1;
44                 rank1[2]=v2;
45                 rank1[3]=v3;
46         }
47         /**
48         * Compares two tensors for equality.
49         * @param a a 1st rank tensor
50         */

51         public boolean equals(Object JavaDoc a) {
52                 if(a instanceof Rank1Tensor) {
53                         Rank1Tensor v=(Rank1Tensor)a;
54                         return Math.abs(rank1[0]-v.rank1[0])<=GlobalSettings.ZERO_TOL &&
55                                 Math.abs(rank1[1]-v.rank1[1])<=GlobalSettings.ZERO_TOL &&
56                                 Math.abs(rank1[2]-v.rank1[2])<=GlobalSettings.ZERO_TOL &&
57                                 Math.abs(rank1[3]-v.rank1[3])<=GlobalSettings.ZERO_TOL;
58                 }
59                 return false;
60         }
61         /**
62         * Returns a comma delimited string representing the value of this tensor.
63         */

64         public String JavaDoc toString() {
65                 return new String JavaDoc(rank1[0]+","+rank1[1]+","+rank1[2]+","+rank1[3]);
66         }
67         /**
68         * Returns a hashcode for this tensor.
69         */

70         public int hashCode() {
71                 return (int)Math.exp(norm());
72         }
73         /**
74         * Returns a component of this tensor.
75         * @param i 1st index
76         * @exception DimensionException If attempting to access an invalid component.
77         */

78         public double getComponent(int i) {
79                 if(i>=0 && i<4)
80                         return rank1[i];
81                 else
82                         throw new DimensionException("Invalid component.");
83         }
84         /**
85         * Sets the value of a component of this tensor.
86         * @param i 1st index
87         * @param x value
88         * @exception DimensionException If attempting to access an invalid component.
89         */

90         public void setComponent(int i,double x) {
91                 if(i>=0 && i<4)
92                         rank1[i]=x;
93                 else
94                         throw new DimensionException("Invalid component.");
95         }
96         /**
97         * Returns the norm (invariant).
98         */

99         public double norm() {
100                 return Math.sqrt(rank1[0]*rank1[0]-rank1[1]*rank1[1]
101                         -rank1[2]*rank1[2]-rank1[3]*rank1[3]);
102         }
103
104 //============
105
// OPERATIONS
106
//============
107

108 // ADDITION
109

110         /**
111         * Returns the addition of this tensor and another.
112         * @param t a 1st rank tensor
113         */

114         public Rank1Tensor add(Rank1Tensor t) {
115                 Rank1Tensor ans=new Rank1Tensor();
116                 ans.rank1[0]=rank1[0]+t.rank1[0];
117                 ans.rank1[1]=rank1[1]+t.rank1[1];
118                 ans.rank1[2]=rank1[2]+t.rank1[2];
119                 ans.rank1[3]=rank1[3]+t.rank1[3];
120                 return ans;
121         }
122
123 // SUBTRACTION
124

125         /**
126         * Returns the subtraction of this tensor by another.
127         * @param t a 1st rank tensor
128         */

129         public Rank1Tensor subtract(Rank1Tensor t) {
130                 Rank1Tensor ans=new Rank1Tensor();
131                 ans.rank1[0]=rank1[0]-t.rank1[0];
132                 ans.rank1[1]=rank1[1]-t.rank1[1];
133                 ans.rank1[2]=rank1[2]-t.rank1[2];
134                 ans.rank1[3]=rank1[3]-t.rank1[3];
135                 return ans;
136         }
137
138 // TENSOR PRODUCT
139

140         /**
141         * Returns the tensor product of this tensor and another.
142         * @param t a 1st rank tensor
143         */

144         public Rank2Tensor tensorProduct(Rank1Tensor t) {
145                 Rank2Tensor ans=new Rank2Tensor();
146                 for(int i=0;i<4;i++) {
147                         ans.setComponent(i,0,rank1[i]*t.rank1[0]);
148                         ans.setComponent(i,1,rank1[i]*t.rank1[1]);
149                         ans.setComponent(i,2,rank1[i]*t.rank1[2]);
150                         ans.setComponent(i,3,rank1[i]*t.rank1[3]);
151                 }
152                 return ans;
153         }
154         /**
155         * Returns the tensor product of this tensor and another.
156         * @param t a 2nd rank tensor
157         */

158         public Rank3Tensor tensorProduct(Rank2Tensor t) {
159                 Rank3Tensor ans=new Rank3Tensor();
160                 for(int j,i=0;i<4;i++) {
161                         for(j=0;j<4;j++) {
162                                 ans.setComponent(i,j,0,rank1[i]*t.getComponent(j,0));
163                                 ans.setComponent(i,j,1,rank1[i]*t.getComponent(j,1));
164                                 ans.setComponent(i,j,2,rank1[i]*t.getComponent(j,2));
165                                 ans.setComponent(i,j,3,rank1[i]*t.getComponent(j,3));
166                         }
167                 }
168                 return ans;
169         }
170         /**
171         * Returns the tensor product of this tensor and another.
172         * @param t a 3rd rank tensor
173         */

174         public Rank4Tensor tensorProduct(Rank3Tensor t) {
175                 Rank4Tensor ans=new Rank4Tensor();
176                 for(int k,j,i=0;i<4;i++) {
177                         for(j=0;j<4;j++) {
178                                 for(k=0;k<4;k++) {
179                                         ans.setComponent(i,j,k,0,rank1[i]*t.getComponent(j,k,0));
180                                         ans.setComponent(i,j,k,1,rank1[i]*t.getComponent(j,k,1));
181                                         ans.setComponent(i,j,k,2,rank1[i]*t.getComponent(j,k,2));
182                                         ans.setComponent(i,j,k,3,rank1[i]*t.getComponent(j,k,3));
183                                 }
184                         }
185                 }
186                 return ans;
187         }
188 }
189
190
Popular Tags