KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 public class Rank2Tensor extends Tensor {
12         protected double rank2[][];
13
14         /**
15         * Constructs a 2nd rank tensor.
16         */

17         public Rank2Tensor() {
18                 rank2=new double[4][4];
19         }
20         /**
21         * Compares two tensors for equality.
22         * @param a a 2nd rank tensor
23         */

24         public boolean equals(Object JavaDoc a) {
25                 if(a instanceof Rank2Tensor) {
26                         Rank2Tensor v=(Rank2Tensor)a;
27                         for(int i=0;i<4;i++) {
28                                 if(Math.abs(rank2[i][0]-v.rank2[i][0])>GlobalSettings.ZERO_TOL ||
29                                         Math.abs(rank2[i][1]-v.rank2[i][1])>GlobalSettings.ZERO_TOL ||
30                                         Math.abs(rank2[i][2]-v.rank2[i][2])>GlobalSettings.ZERO_TOL ||
31                                         Math.abs(rank2[i][3]-v.rank2[i][3])>GlobalSettings.ZERO_TOL)
32                                         return false;
33                         }
34                         return true;
35                 }
36                 return false;
37         }
38         /**
39         * Returns a component of this tensor.
40         * @param i 1st index
41         * @param j 2nd index
42         * @exception DimensionException If attempting to access an invalid component.
43         */

44         public double getComponent(int i, int j) {
45                 if(i>=0 && i<4 && j>=0 && j<4)
46                         return rank2[i][j];
47                 else
48                         throw new DimensionException("Invalid component.");
49         }
50         /**
51         * Sets the value of a component of this tensor.
52         * @param i 1st index
53         * @param j 2nd index
54         * @param x value
55         * @exception DimensionException If attempting to access an invalid component.
56         */

57         public void setComponent(int i,int j,double x) {
58                 if(i>=0 && i<4 && j>=0 && j<4)
59                         rank2[i][j]=x;
60                 else
61                         throw new DimensionException("Invalid component.");
62         }
63
64 //============
65
// OPERATIONS
66
//============
67

68 // ADDITION
69

70        /**
71         * Returns the addition of this tensor and another.
72         * @param t a 2nd rank tensor
73         */

74         public Rank2Tensor add(Rank2Tensor t) {
75                 Rank2Tensor ans=new Rank2Tensor();
76                 for(int i=0;i<4;i++) {
77                         ans.setComponent(i,0,rank2[i][0]+t.rank2[i][0]);
78                         ans.setComponent(i,1,rank2[i][1]+t.rank2[i][1]);
79                         ans.setComponent(i,2,rank2[i][2]+t.rank2[i][2]);
80                         ans.setComponent(i,3,rank2[i][3]+t.rank2[i][3]);
81                 }
82                 return ans;
83         }
84
85 // SUBTRACTION
86

87        /**
88         * Returns the subtraction of this tensor by another.
89         * @param t a 2nd rank tensor
90         */

91         public Rank2Tensor subtract(Rank2Tensor t) {
92                 Rank2Tensor ans=new Rank2Tensor();
93                 for(int i=0;i<4;i++) {
94                         ans.setComponent(i,0,rank2[i][0]-t.rank2[i][0]);
95                         ans.setComponent(i,1,rank2[i][1]-t.rank2[i][1]);
96                         ans.setComponent(i,2,rank2[i][2]-t.rank2[i][2]);
97                         ans.setComponent(i,3,rank2[i][3]-t.rank2[i][3]);
98                 }
99                 return ans;
100         }
101
102 // MULTIPLY
103

104         /**
105         * Returns the multiplication of this tensor by another.
106         * @param t a 1st rank tensor
107         */

108         public Rank1Tensor multiply(Rank1Tensor t) {
109                 Rank1Tensor ans=new Rank1Tensor();
110                 ans.setComponent(0,rank2[0][0]*t.getComponent(0)+rank2[0][1]*t.getComponent(1)+rank2[0][2]*t.getComponent(2)+rank2[0][3]*t.getComponent(3));
111                 ans.setComponent(1,rank2[1][0]*t.getComponent(0)+rank2[1][1]*t.getComponent(1)+rank2[1][2]*t.getComponent(2)+rank2[1][3]*t.getComponent(3));
112                 ans.setComponent(2,rank2[2][0]*t.getComponent(0)+rank2[2][1]*t.getComponent(1)+rank2[2][2]*t.getComponent(2)+rank2[2][3]*t.getComponent(3));
113                 ans.setComponent(3,rank2[3][0]*t.getComponent(0)+rank2[3][1]*t.getComponent(1)+rank2[3][2]*t.getComponent(2)+rank2[3][3]*t.getComponent(3));
114                 return ans;
115         }
116
117 // TENSOR PRODUCT
118

119         /**
120         * Returns the tensor product of this tensor and another.
121         * @param t a 1st rank tensor
122         */

123         public Rank3Tensor tensorProduct(Rank1Tensor t) {
124                 Rank3Tensor ans=new Rank3Tensor();
125                 for(int j,i=0;i<4;i++) {
126                         for(j=0;j<4;j++) {
127                                 ans.setComponent(i,j,0,rank2[i][j]*t.getComponent(0));
128                                 ans.setComponent(i,j,1,rank2[i][j]*t.getComponent(1));
129                                 ans.setComponent(i,j,2,rank2[i][j]*t.getComponent(2));
130                                 ans.setComponent(i,j,3,rank2[i][j]*t.getComponent(3));
131                         }
132                 }
133                 return ans;
134         }
135         /**
136         * Returns the tensor product of this tensor and another.
137         * @param t a 2nd rank tensor
138         */

139         public Rank4Tensor tensorProduct(Rank2Tensor t) {
140                 Rank4Tensor ans=new Rank4Tensor();
141                 for(int k,j,i=0;i<4;i++) {
142                         for(j=0;j<4;j++) {
143                                 for(k=0;k<4;k++) {
144                                         ans.setComponent(i,j,k,0,rank2[i][j]*t.rank2[k][0]);
145                                         ans.setComponent(i,j,k,1,rank2[i][j]*t.rank2[k][1]);
146                                         ans.setComponent(i,j,k,2,rank2[i][j]*t.rank2[k][2]);
147                                         ans.setComponent(i,j,k,3,rank2[i][j]*t.rank2[k][3]);
148                                 }
149                         }
150                 }
151                 return ans;
152         }
153 }
154
155
Popular Tags