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 FloatComplex 29 { 30 private final int offset; 31 private final FloatBuffer buffer; 32 33 private FloatComplex(FloatBuffer 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 FloatComplex get(FloatBuffer buffer) 45 { 46 return new FloatComplex(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 FloatComplex get(FloatBuffer buffer, int index) 57 { 58 return new FloatComplex(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 FloatComplex get(float[] array) 67 { 68 return new FloatComplex(FloatBuffer.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 floats. 74 * @param array array 75 * @param index index 76 * @return complex number 77 */ 78 public static FloatComplex get(float[] array, int index) 79 { 80 return new FloatComplex(FloatBuffer.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 FloatComplex get(ByteBuffer buffer) 89 { 90 return new FloatComplex(buffer.asFloatBuffer(), 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 FloatComplex get(ByteBuffer buffer, int index) 101 { 102 return new FloatComplex(buffer.asFloatBuffer(), index); 103 } 104 105 /** 106 * Gets the real value. 107 * @return real value 108 */ 109 public float getReal() 110 { 111 return buffer.get(offset); 112 } 113 114 /** 115 * Gets the imaginary value. 116 * @return imaginary value. 117 */ 118 public float 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(float 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(float 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 FloatBuffer getBuffer() 146 { 147 return offset == 0 ? buffer : MPI.slice(buffer, offset); 148 } 149 150 } // FloatComplex