KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 public class Rank4Tensor extends Tensor {
12         protected double rank4[][][][];
13
14         /**
15         * Constructs a 4th rank tensor.
16         */

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

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

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

65         public void setComponent(int i,int j,int k,int l,double x) {
66                 if(i>=0 && i<4 && j>=0 && j<4 && k>=0 && k<4 && l>=0 && l<4)
67                         rank4[i][j][k][l]=x;
68                 else
69                         throw new DimensionException("Invalid component.");
70         }
71
72 //============
73
// OPERATIONS
74
//============
75

76 // ADDITION
77

78         /**
79         * Returns the addition of this tensor and another.
80         * @param t a 4th rank tensor
81         */

82         public Rank4Tensor add(Rank4Tensor t) {
83                 Rank4Tensor ans=new Rank4Tensor();
84                 for(int k,j,i=0;i<4;i++) {
85                         for(j=0;j<4;j++) {
86                                 for(k=0;k<4;k++) {
87                                         ans.setComponent(i,j,k,0,rank4[i][j][k][0]+t.rank4[i][j][k][0]);
88                                         ans.setComponent(i,j,k,1,rank4[i][j][k][1]+t.rank4[i][j][k][1]);
89                                         ans.setComponent(i,j,k,2,rank4[i][j][k][2]+t.rank4[i][j][k][2]);
90                                         ans.setComponent(i,j,k,3,rank4[i][j][k][3]+t.rank4[i][j][k][3]);
91                                 }
92                         }
93                 }
94                 return ans;
95         }
96
97 // SUBTRACTION
98

99         /**
100         * Returns the subtraction of this tensor by another.
101         * @param t a 4th rank tensor
102         */

103         public Rank4Tensor subtract(Rank4Tensor t) {
104                 Rank4Tensor ans=new Rank4Tensor();
105                 for(int k,j,i=0;i<4;i++) {
106                         for(j=0;j<4;j++) {
107                                 for(k=0;k<4;k++) {
108                                         ans.setComponent(i,j,k,0,rank4[i][j][k][0]-t.rank4[i][j][k][0]);
109                                         ans.setComponent(i,j,k,1,rank4[i][j][k][1]-t.rank4[i][j][k][1]);
110                                         ans.setComponent(i,j,k,2,rank4[i][j][k][2]-t.rank4[i][j][k][2]);
111                                         ans.setComponent(i,j,k,3,rank4[i][j][k][3]-t.rank4[i][j][k][3]);
112                                 }
113                         }
114                 }
115                 return ans;
116         }
117 }
118
119
Popular Tags