~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/math/IVector.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* IVector.java
 
2
 * 
 
3
 * Autor: Stephan Michels 
 
4
 * EMail: stephan@vern.chem.tu-berlin.de
 
5
 * Datum: 22.7.2001
 
6
 * 
 
7
 * Copyright (C) 1997-2007  The Chemistry Development Kit (CDK) project
 
8
 * 
 
9
 * Contact: cdk-devel@lists.sourceforge.net
 
10
 * 
 
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.
 
19
 * 
 
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.
 
24
 * 
 
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.
 
28
 *  */
 
29
 
 
30
package org.openscience.cdk.math;
 
31
 
 
32
public class IVector
 
33
{
 
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;
 
39
 
 
40
  /** Size of this vector */
 
41
  public int size;
 
42
 
 
43
  /**
 
44
   * Constructs a vector with "size"-elements
 
45
   */
 
46
  public IVector(int size)
 
47
  {
 
48
    realvector = new double[size];
 
49
    imagvector = new double[size];
 
50
    this.size = size;
 
51
  }
 
52
 
 
53
  /**
 
54
   * Return the size from this vector
 
55
   */
 
56
  public int getSize()
 
57
  {
 
58
    return size;
 
59
  }
 
60
 
 
61
  /**
 
62
   *  Addition from two vectors
 
63
   */
 
64
  public IVector add(IVector b)
 
65
  {
 
66
    IVector result = new IVector(size);
 
67
    add(b, result);
 
68
    return result;
 
69
  }
 
70
 
 
71
  /**
 
72
   *  Addition from two vectors
 
73
   */
 
74
  public void add(IVector b, IVector result)
 
75
  {
 
76
    if ((b==null) ||
 
77
         (size!=b.size))
 
78
      return;
 
79
      
 
80
    if (result.size!=size)
 
81
      result.reshape(size);
 
82
 
 
83
    int i;
 
84
    for(i=0; i<size; i++)
 
85
    {
 
86
      result.realvector[i] = realvector[i]+b.realvector[i];
 
87
      result.imagvector[i] = imagvector[i]+b.imagvector[i];
 
88
    }
 
89
  }
 
90
 
 
91
  /**
 
92
   *  Subtraction from two vectors
 
93
   */
 
94
  public IVector sub(IVector b)
 
95
  {
 
96
    IVector result = new IVector(size);
 
97
    sub(b, result); 
 
98
    return result;
 
99
  } 
 
100
  
 
101
  /**
 
102
   *  Subtraction from two vectors
 
103
   */
 
104
  public void sub(IVector b, IVector result)
 
105
  {
 
106
    if ((b==null) ||
 
107
        (size!=b.size))
 
108
      return;
 
109
      
 
110
    if (result.size!=size)
 
111
      result.reshape(size);
 
112
 
 
113
    int i;
 
114
    for(i=0; i<size; i++)
 
115
    {
 
116
      result.realvector[i] = realvector[i]-b.realvector[i];
 
117
      result.imagvector[i] = imagvector[i]-b.imagvector[i];
 
118
    }
 
119
  }
 
120
  
 
121
  /**
 
122
   *  Multiplication from a vectors with an double
 
123
   */
 
124
  public IVector mul(double b)
 
125
  {
 
126
    IVector result = new IVector(size);
 
127
    mul(b, result); 
 
128
    return result;
 
129
  } 
 
130
  
 
131
  /**
 
132
   *  Multiplication from a vectors with an double
 
133
   */
 
134
  public void mul(double b, IVector result)
 
135
  {
 
136
    if (result.size!=size)
 
137
      result.reshape(size);
 
138
 
 
139
    int i;
 
140
    for(i=0; i<size; i++)
 
141
    {
 
142
      result.realvector[i] = realvector[i]*b;
 
143
      result.imagvector[i] = imagvector[i]*b;
 
144
    }
 
145
  }
 
146
 
 
147
  /**
 
148
   *  Multiplication from two vectors
 
149
   */
 
150
  public Complex dot(IVector b)
 
151
  {
 
152
    if ((b==null) ||
 
153
        (size!=b.size))
 
154
      return new Complex(Double.NaN,Double.NaN);
 
155
 
 
156
    Complex result = new Complex(0d,0d);
 
157
    int i;
 
158
    for(i=0; i<size; i++)
 
159
    {
 
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];
 
162
    }
 
163
    return result;
 
164
  }
 
165
 
 
166
  /**
 
167
   *  Copy a vector
 
168
   */
 
169
  public IVector duplicate()
 
170
  {
 
171
    IVector result = new IVector(size);
 
172
    duplicate(result); 
 
173
    return result;
 
174
  } 
 
175
  
 
176
  /**
 
177
   *  Copy a vector
 
178
   */
 
179
  public void duplicate(IVector result)
 
180
  { 
 
181
    if (result.size!=size)
 
182
      result.reshape(size);
 
183
 
 
184
    int i; 
 
185
    for(i=0; i<size; i++)
 
186
    {
 
187
      result.realvector[i] = realvector[i];
 
188
      result.imagvector[i] = imagvector[i];
 
189
    }
 
190
  }
 
191
  
 
192
  /**
 
193
   *  Return a vector as a string
 
194
   */
 
195
  public String toString()
 
196
  {
 
197
    int i;
 
198
    StringBuffer str = new StringBuffer();
 
199
    str.append("[ ");
 
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();
 
204
  }
 
205
 
 
206
  /**
 
207
   * Resize this vector
 
208
   */
 
209
  public void reshape(int newsize)
 
210
  {
 
211
    if ((newsize==size) || (newsize<=0))
 
212
      return;
 
213
 
 
214
    double[] newrealvector = new double[newsize];
 
215
    double[] newimagvector = new double[newsize];
 
216
    int min = Math.min(size,newsize);
 
217
    int i;
 
218
    for(i=0; i<min; i++)
 
219
    {
 
220
      newrealvector[i] = realvector[i];
 
221
      newimagvector[i] = imagvector[i];
 
222
    }
 
223
    for(i=min; i<newsize; i++)  
 
224
    {
 
225
      newrealvector[i] = 0d;
 
226
      newimagvector[i] = 0d;
 
227
    }
 
228
 
 
229
    realvector = newrealvector;
 
230
    imagvector = newimagvector;
 
231
    size = newsize;
 
232
  }
 
233
}