Debian Codegraph @

src/dr/oldevomodel/indel/IntMathVec.java

raw symbols SCIP
1 /*
2 * IntMathVec.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.oldevomodel.indel;
27
28 /**
29 *
30 * IntMathVec -- mathematical vector of integers.
31 *
32 * Vectors provide equals() and hashCode() based on value-identity,
33 * so can be used in maps, but are NOT immutable, so take care not
34 * to change the value of map-keys.
35 *
36 * @author Gerton Lunter
37 *
38 * 20/3/2003
39 *
40 */
41
42 class IntMathVec implements Cloneable {
43 public int[] iV;
44
45 /** Constructor, null vector of given length */
46 IntMathVec(int iLen) {
47 iV = new int[iLen];
48 }
49
50 /** Constructor, copy of existing vector */
51 IntMathVec(IntMathVec iVec) {
52 iV = iVec.iV.clone();
53 }
54
55 /** Constructor, copy of integer array - handy for printing */
56 IntMathVec(int[] iArr) {
57 iV = iArr.clone();
58 }
59
60 /** Internal, to make sure vectors are of same length. */
61 // I know, I should throw an exception.. Will likely happen automatically.
62 private void check(IntMathVec iVec) {
63 if (iVec.iV.length != iV.length) {
64 System.out.println("IntMathVec.check: Vector sizes don't match.");
65 }
66 }
67
68 /** Make equals() reflect value-identity instead of object-identity */
69 public boolean equals(Object iObj) {
70 if (iObj instanceof IntMathVec) {
71 IntMathVec iVec = (IntMathVec)iObj;
72 if (iVec.iV.length != iV.length)
73 return false;
74 for (int i=0; i<iV.length; i++) {
75 if (iV[i] != iVec.iV[i])
76 return false;
77 }
78 return true;
79 }
80 return false;
81 }
82
83 /** hashCode() reflects value-identity instead of object-identity */
84 public int hashCode() {
85 int iCode = 0;
86 for( int anIV : iV ) {
87 iCode = (iCode * 75) + anIV;
88 }
89 return iCode;
90 }
91
92 /** Clone this object */
93 public IntMathVec clone() {
94 try {
95 // This magically creates an object of the right type
96 IntMathVec iObj = (IntMathVec) super.clone();
97 iObj.iV = iV.clone();
98 return iObj;
99 } catch (CloneNotSupportedException e) {
100 System.out.println("IntMathVec.clone: Something happened that cannot happen -- ?");
101 return null;
102 }
103 }
104
105 /** Overriding built-in toString, produces Mathematica-readable output */
106 public String toString() {
107 String iResult = "{";
108 for (int i=0; i<iV.length; i++) {
109 if (i != 0)
110 iResult += ",";
111 iResult += iV[i];
112 }
113 iResult += "}";
114 return iResult;
115 }
116
117 /** Now the math thingies */
118 public int innerProduct(IntMathVec iVec) {
119 check(iVec);
120 int iSum = 0;
121 for (int i=0; i<iV.length; i++)
122 iSum += iVec.iV[i]*iV[i];
123 return iSum;
124 }
125
126 public boolean zeroEntry() {
127 for(int anIV : iV) {
128 if( anIV == 0 ) {
129 return true;
130 }
131 }
132 return false;
133 }
134
135 public void assign(IntMathVec iVec) {
136 iV = iVec.iV.clone();
137 }
138
139 public void add(IntMathVec iVec) {
140 check(iVec);
141 for (int i=0; i<iV.length; i++)
142 iV[i] += iVec.iV[i];
143 }
144
145 public void addMultiple(IntMathVec iVec, int iMultiple) {
146 check(iVec);
147 for (int i=0; i<iV.length; i++)
148 iV[i] += iVec.iV[i] * iMultiple;
149 }
150
151 public void subtract(IntMathVec iVec) {
152 check(iVec);
153 for (int i=0; i<iV.length; i++)
154 iV[i] -= iVec.iV[i];
155 }
156
157 }
158
159
160