3
* Autor: Stephan Michels
4
* EMail: stephan@vern.chem.tu-berlin.de
7
* Copyright (C) 1997-2007 The Chemistry Development Kit (CDK) project
9
* Contact: cdk-devel@lists.sourceforge.net
11
* This program is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public License
13
* as published by the Free Software Foundation; either version 2.1
14
* of the License, or (at your option) any later version.
15
* All we ask is that proper credit is given for our work, which includes
16
* - but is not limited to - adding the above copyright notice to the beginning
17
* of your source code files, and to any copyright notice that you may distribute
18
* with programs based on this work.
20
* This program is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
* GNU Lesser General Public License for more details.
25
* You should have received a copy of the GNU Lesser General Public License
26
* along with this program; if not, write to the Free Software
27
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30
package org.openscience.cdk.math;
34
// Attention! The variables are unprotected
35
/** The real part of this vector */
36
public double[] realvector;
37
/** The imaginary part of this vector */
38
public double[] imagvector;
40
/** Size of this vector */
44
* Constructs a vector with "size"-elements
46
public IVector(int size)
48
realvector = new double[size];
49
imagvector = new double[size];
54
* Return the size from this vector
62
* Addition from two vectors
64
public IVector add(IVector b)
66
IVector result = new IVector(size);
72
* Addition from two vectors
74
public void add(IVector b, IVector result)
80
if (result.size!=size)
86
result.realvector[i] = realvector[i]+b.realvector[i];
87
result.imagvector[i] = imagvector[i]+b.imagvector[i];
92
* Subtraction from two vectors
94
public IVector sub(IVector b)
96
IVector result = new IVector(size);
102
* Subtraction from two vectors
104
public void sub(IVector b, IVector result)
110
if (result.size!=size)
111
result.reshape(size);
114
for(i=0; i<size; i++)
116
result.realvector[i] = realvector[i]-b.realvector[i];
117
result.imagvector[i] = imagvector[i]-b.imagvector[i];
122
* Multiplication from a vectors with an double
124
public IVector mul(double b)
126
IVector result = new IVector(size);
132
* Multiplication from a vectors with an double
134
public void mul(double b, IVector result)
136
if (result.size!=size)
137
result.reshape(size);
140
for(i=0; i<size; i++)
142
result.realvector[i] = realvector[i]*b;
143
result.imagvector[i] = imagvector[i]*b;
148
* Multiplication from two vectors
150
public Complex dot(IVector b)
154
return new Complex(Double.NaN,Double.NaN);
156
Complex result = new Complex(0d,0d);
158
for(i=0; i<size; i++)
160
result.real += realvector[i]*b.realvector[i]-imagvector[i]*b.imagvector[i];
161
result.imag += realvector[i]*b.imagvector[i]+imagvector[i]*b.realvector[i];
169
public IVector duplicate()
171
IVector result = new IVector(size);
179
public void duplicate(IVector result)
181
if (result.size!=size)
182
result.reshape(size);
185
for(i=0; i<size; i++)
187
result.realvector[i] = realvector[i];
188
result.imagvector[i] = imagvector[i];
193
* Return a vector as a string
195
public String toString()
198
StringBuffer str = new StringBuffer();
200
for(i=0; i<(size-1); i++)
201
str.append(realvector[i]+"+i*"+imagvector[i]+" ");
202
str.append(realvector[size-1]+"+i*"+imagvector[size-1]+" ]");
203
return str.toString();
209
public void reshape(int newsize)
211
if ((newsize==size) || (newsize<=0))
214
double[] newrealvector = new double[newsize];
215
double[] newimagvector = new double[newsize];
216
int min = Math.min(size,newsize);
220
newrealvector[i] = realvector[i];
221
newimagvector[i] = imagvector[i];
223
for(i=min; i<newsize; i++)
225
newrealvector[i] = 0d;
226
newimagvector[i] = 0d;
229
realvector = newrealvector;
230
imagvector = newimagvector;