1 /*
2 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
3 * University Research and Technology
4 * Corporation. All rights reserved.
5 * Copyright (c) 2004-2005 The University of Tennessee and The University
6 * of Tennessee Research Foundation. All rights
7 * reserved.
8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9 * University of Stuttgart. All rights reserved.
10 * Copyright (c) 2004-2005 The Regents of the University of California.
11 * All rights reserved.
12 * Copyright (c) 2015 Los Alamos National Security, LLC. All rights
13 * reserved.
14 * $COPYRIGHT$
15 *
16 * Additional copyrights may follow
17 *
18 * $HEADER$
19 */
20
21 package mpi;
22
23 import java.nio.*;
24
25 /**
26 * This class wraps a complex number stored in a buffer.
27 */
28 public final class DoubleComplex
29 {
30 private final int offset;
31 private final DoubleBuffer buffer;
32
33 private DoubleComplex(DoubleBuffer buffer, int index)
34 {
35 this.buffer = buffer;
36 this.offset = index * 2;
37 }
38
39 /**
40 * Wraps a complex number stored in a buffer
41 * @param buffer buffer
42 * @return complex number
43 */
44 public static DoubleComplex get(DoubleBuffer buffer)
45 {
46 return new DoubleComplex(buffer, 0);
47 }
48
49 /**
50 * Wraps the complex number at the specified position
51 * of an array of complex numbers stored in a buffer.
52 * @param buffer buffer
53 * @param index index
54 * @return complex number
55 */
56 public static DoubleComplex get(DoubleBuffer buffer, int index)
57 {
58 return new DoubleComplex(buffer, index);
59 }
60
61 /**
62 * Wraps a complex number stored in the first two values of an array.
63 * @param array array
64 * @return complex number
65 */
66 public static DoubleComplex get(double[] array)
67 {
68 return new DoubleComplex(DoubleBuffer.wrap(array), 0);
69 }
70
71 /**
72 * Wraps the complex number at the specified position of
73 * an array of complex numbers stored in an array of doubles.
74 * @param array array
75 * @param index index
76 * @return complex number
77 */
78 public static DoubleComplex get(double[] array, int index)
79 {
80 return new DoubleComplex(DoubleBuffer.wrap(array), index);
81 }
82
83 /**
84 * Wraps a complex number stored in a buffer
85 * @param buffer buffer
86 * @return complex number
87 */
88 public static DoubleComplex get(ByteBuffer buffer)
89 {
90 return new DoubleComplex(buffer.asDoubleBuffer(), 0);
91 }
92
93 /**
94 * Wraps the complex number at the specified position
95 * of an array of complex numbers stored in a buffer.
96 * @param buffer buffer
97 * @param index index
98 * @return complex number
99 */
100 public static DoubleComplex get(ByteBuffer buffer, int index)
101 {
102 return new DoubleComplex(buffer.asDoubleBuffer(), index);
103 }
104
105 /**
106 * Gets the real value.
107 * @return real value
108 */
109 public double getReal()
110 {
111 return buffer.get(offset);
112 }
113
114 /**
115 * Gets the imaginary value.
116 * @return imaginary value.
117 */
118 public double getImag()
119 {
120 return buffer.get(offset + 1);
121 }
122
123 /**
124 * Puts the real value.
125 * @param real real value
126 */
127 public void putReal(double real)
128 {
129 buffer.put(offset, real);
130 }
131
132 /**
133 * Puts the imaginary value.
134 * @param imag imaginary value
135 */
136 public void putImag(double imag)
137 {
138 buffer.put(offset + 1, imag);
139 }
140
141 /**
142 * Gets the buffer where the complex number is stored.
143 * @return buffer where the complex number is stored
144 */
145 public DoubleBuffer getBuffer()
146 {
147 return offset == 0 ? buffer : MPI.slice(buffer, offset);
148 }
149
150 } // DoubleComplex