1 package JSci.physics.relativity; 2 3 import JSci.GlobalSettings; 4 import JSci.maths.*; 5 6 11 public class Rank2Tensor extends Tensor { 12 protected double rank2[][]; 13 14 17 public Rank2Tensor() { 18 rank2=new double[4][4]; 19 } 20 24 public boolean equals(Object 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 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 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 68 70 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 87 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 104 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 119 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 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 |