KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 public class Rank3Tensor extends Tensor {
12         protected double rank3[][][];
13
14         /**
15         * Constructs a 3rd rank tensor.
16         */

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

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

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

61         public void setComponent(int i,int j,int k,double x) {
62                 if(i>=0 && i<4 && j>=0 && j<4 && k>=0 && k<4)
63                         rank3[i][j][k]=x;
64                 else
65                         throw new DimensionException("Invalid component.");
66         }
67
68 //============
69
// OPERATIONS
70
//============
71

72 // ADDITION
73

74         /**
75         * Returns the addition of this tensor and another.
76         * @param t a 3rd rank tensor
77         */

78         public Rank3Tensor add(Rank3Tensor t) {
79                 Rank3Tensor ans=new Rank3Tensor();
80                 for(int j,i=0;i<4;i++) {
81                         for(j=0;j<4;j++) {
82                                 ans.setComponent(i,j,0,rank3[i][j][0]+t.rank3[i][j][0]);
83                                 ans.setComponent(i,j,1,rank3[i][j][1]+t.rank3[i][j][1]);
84                                 ans.setComponent(i,j,2,rank3[i][j][2]+t.rank3[i][j][2]);
85                                 ans.setComponent(i,j,3,rank3[i][j][3]+t.rank3[i][j][3]);
86                         }
87                 }
88                 return ans;
89         }
90
91 // SUBTRACTION
92

93         /**
94         * Returns the subtraction of this tensor by another.
95         * @param t a 3rd rank tensor
96         */

97         public Rank3Tensor subtract(Rank3Tensor t) {
98                 Rank3Tensor ans=new Rank3Tensor();
99                 for(int j,i=0;i<4;i++) {
100                         for(j=0;j<4;j++) {
101                                 ans.setComponent(i,j,0,rank3[i][j][0]-t.rank3[i][j][0]);
102                                 ans.setComponent(i,j,1,rank3[i][j][1]-t.rank3[i][j][1]);
103                                 ans.setComponent(i,j,2,rank3[i][j][2]-t.rank3[i][j][2]);
104                                 ans.setComponent(i,j,3,rank3[i][j][3]-t.rank3[i][j][3]);
105                         }
106                 }
107                 return ans;
108         }
109
110 // TENSOR PRODUCT
111

112         /**
113         * Returns the tensor product of this tensor and another.
114         * @param t a 1st rank tensor
115         */

116         public Rank4Tensor tensorProduct(Rank1Tensor t) {
117                 Rank4Tensor ans=new Rank4Tensor();
118                 for(int k,j,i=0;i<4;i++) {
119                         for(j=0;j<4;j++) {
120                                 for(k=0;k<4;k++) {
121                                         ans.setComponent(i,j,k,0,rank3[i][j][k]*t.getComponent(0));
122                                         ans.setComponent(i,j,k,1,rank3[i][j][k]*t.getComponent(1));
123                                         ans.setComponent(i,j,k,2,rank3[i][j][k]*t.getComponent(2));
124                                         ans.setComponent(i,j,k,3,rank3[i][j][k]*t.getComponent(3));
125                                 }
126                         }
127                 }
128                 return ans;
129         }
130 }
131
132
Popular Tags