Debian Codegraph @

src/dr/math/ComplexArray.java

raw symbols SCIP
1 /*
2 * ComplexArray.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.math;
27
28 import dr.math.matrixAlgebra.Vector;
29
30 /**
31 * @author Marc A. Suchard
32 */
33 public class ComplexArray {
34
35 public final double[] real;
36 public final double[] complex;
37 public final int length;
38
39 public ComplexArray(double[] real) {
40 this(real, new double[real.length]);
41 }
42
43 public ComplexArray(double[] real, double[] complex) {
44 this.real = real;
45 this.complex = complex;
46 this.length = real.length;
47 }
48
49 public void conjugate() {
50 for (int i = 0; i < length; ++i) {
51 complex[i] = -complex[i];
52 }
53 }
54
55 public ComplexArray product(ComplexArray x) {
56 double[] newReal = new double[length];
57 double[] newComplex = new double[length];
58 for (int i = 0; i < length; ++i) {
59 // (a + bi) * (c + di) = (ac - bd) + (ad + bc)i
60 final double a = real[i];
61 final double b = complex[i];
62 final double c = x.real[i];
63 final double d = x.complex[i];
64 newReal[i] = a * c - b * d;
65 newComplex[i] = a * d + b * c;
66 }
67 return new ComplexArray(newReal, newComplex);
68 }
69
70 public String toString() {
71 return "\nReal : " + new Vector(real).toString() +
72 "\nComplex: " + new Vector(complex).toString();
73 }
74
75 public static double[] interleave(double[] real, double[] complex) {
76 final int length = real.length;
77 double[] out = new double[length * 2];
78 for (int i = 0; i < length; i++) {
79 out[2 * i] = real[i];
80 out[2 * i + 1] = complex[i];
81 }
82 return out;
83 }
84 }