Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > JSci > physics > quantum > Operator

 `1 package JSci.physics.quantum;2 3 import JSci.maths.Complex;4 import JSci.maths.Member;5 import JSci.maths.DimensionException;6 import JSci.maths.MaximumIterationsExceededException;7 import JSci.maths.matrices.AbstractComplexSquareMatrix;8 import JSci.maths.vectors.AbstractComplexVector;9 import JSci.maths.vectors.ComplexVector;10 11 /**12 * The Operator class provides an object for encapsulating quantum mechanical operators.13 * @version 1.514 * @author Mark Hale15 */16 public class Operator implements Member {17         protected AbstractComplexSquareMatrix representation;18 19         /**20         * Constructs an operator given a matrix representation.21         * @param rep a matrix representation22         */23         public Operator(AbstractComplexSquareMatrix rep) {24                 representation=rep;25         }26         /**27         * Compares two operators for equality.28         * @param a an operator29         */30         public boolean equals(Object a) {31                 return representation.equals(((Operator)a).representation);32         }33         /**34         * Returns a string representing this operator.35         */36         public String toString() {37                 return representation.toString();38         }39         /**40         * Returns a hashcode for this operator.41         */42         public int hashCode() {43                 return (int)Math.exp(trace().mod());44         }45         /**46         * Returns the representation.47         */48         public AbstractComplexSquareMatrix getRepresentation() {49                 return representation;50         }51         /**52         * Returns true if this operator is self-adjoint.53         */54         public boolean isSelfAdjoint() {55                 return representation.isHermitian();56         }57         /**58         * Returns true if this operator is unitary.59         */60         public boolean isUnitary() {61                 return representation.isUnitary();62         }63         /**64         * Returns the trace.65         */66         public Complex trace() {67                 return representation.trace();68         }69         /**70         * Returns the operator norm.71         */72         public double norm() {73                 try {74                         return representation.operatorNorm();75                 } catch(MaximumIterationsExceededException e) {76                         return -1.0;77                 }78         }79         /**80         * Returns the dimension.81         */82         public int dimension() {83                 return representation.columns();84         }85     public Object getSet() {86         return representation.getSet();87     }88 89 //============90 // OPERATIONS91 //============92 93 // ADDITION94 95         /**96         * Returns the addition of this operator and another.97         * @param op an operator98         * @exception MatrixDimensionException If the operators have different dimensions.99         */100         public Operator add(Operator op) {101                 return new Operator(representation.add(op.representation));102         }103 104 // SUBTRACTION105 106         /**107         * Returns the subtraction of this operator and another.108         * @param op an operator109         * @exception MatrixDimensionException If the operators have different dimensions.110         */111         public Operator subtract(Operator op) {112                 return new Operator(representation.subtract(op.representation));113         }114 115 // MULTIPLICATION116 117         /**118         * Returns the multiplication of this operator and another.119         * @param op an operator120         * @exception MatrixDimensionException If the operators have different dimensions.121         */122         public Operator multiply(Operator op) {123                 return new Operator(representation.multiply(op.representation));124         }125         /**126         * Returns the multiplication of this operator and a ket vector.127         * @param ket a ket vector128         * @exception DimensionException If the operator and vector have different dimensions.129         */130         public KetVector multiply(KetVector ket) {131                 int opDim=dimension();132                 if(opDim==ket.dimension()) {133                         AbstractComplexVector ketRep=ket.getRepresentation();134                         Complex tmp,array[]=new Complex[opDim];135                         for(int j,i=0;i