2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2010-2010 - DIGITEO - Clement DAVID
5 * Copyright (C) 2011-2011 - DIGITEO - Calixte DENIZET
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
18 package org.scilab.modules.types;
20 import java.io.ObjectStreamException;
21 import java.nio.DoubleBuffer;
24 * This class provides a representation on the Scilab Double datatype<br>
25 * Note that double is the default datatype in Scilab.<br>
27 * This class is {@link java.io.Serializable} and any modification could impact
28 * load and store of data (Xcos files, Javasci saved data, etc...).<br>
32 * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<BR>
33 * ScilabDouble aMatrix = new ScilabDouble(a);<BR>
35 * Example (complex):<BR>
37 * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<BR>
38 * double [][]aImg={{210.2, 220.0, 420.0, 390.0},{230.2, 240.0, 440.0, 400.0}};<BR>
39 * ScilabDouble aMatrix = new ScilabDouble(a, aImg);
42 * @see org.scilab.modules.javasci.Scilab
44 public class ScilabDoubleReference extends ScilabDouble {
46 private final DoubleBuffer realBuffer;
47 private final DoubleBuffer imaginaryBuffer;
48 private final int nbRows;
49 private final int nbCols;
52 * Constructor with a matrix of complex numbers
55 * the real part of the data
57 * the imaginary part of the data
59 ScilabDoubleReference(String varName, DoubleBuffer realBuffer, DoubleBuffer imagBuffer, int nbRows, int nbCols) {
60 this.realBuffer = realBuffer;
61 this.imaginaryBuffer = imagBuffer;
64 this.varName = varName;
70 * Check the emptiness of the associated data.
72 * @return true, if the associated data array is empty.
75 public boolean isEmpty() {
76 return nbRows == 0 || nbCols == 0;
80 * Check if the current data doesn't have an imaginary part.
82 * @return true, if the data are real only.
85 public boolean isReal() {
86 return imaginaryBuffer == null || imaginaryBuffer.capacity() == 0;
93 public Object getRawRealPart() {
101 public Object getRawImaginaryPart() {
102 return imaginaryBuffer;
106 * Get the real part as DoubleBuffer
107 * @return the real part
109 public DoubleBuffer getRealBuffer() {
114 * Get the imaginary part as DoubleBuffer
115 * @return the imaginary part
117 public DoubleBuffer getImaginaryBuffer() {
118 return imaginaryBuffer;
122 * Get the real part of the data.
124 * @return the real part.
127 public double[][] getRealPart() {
128 double[][] d = new double[nbRows][nbCols];
129 ScilabTypeUtils.setBuffer(d, realBuffer);
134 * Set the real part of the data.
140 public void setRealPart(double[][] realPart) {
141 ScilabTypeUtils.setPart(realBuffer, realPart);
145 * Get the imaginary part of the data.
147 * @return the imaginary part.
150 public double[][] getImaginaryPart() {
151 double[][] d = new double[nbRows][nbCols];
152 ScilabTypeUtils.setBuffer(d, imaginaryBuffer);
157 * Set the imaginary part of the data.
159 * @param imaginaryPart
160 * the imaginary part.
163 public void setImaginaryPart(double[][] imaginaryPart) {
164 ScilabTypeUtils.setPart(imaginaryBuffer, imaginaryPart);
171 public double getRealElement(final int i, final int j) {
172 return realBuffer.get(i + nbRows * j);
179 public double getImaginaryElement(final int i, final int j) {
180 return imaginaryBuffer.get(i + nbRows * j);
187 public void setRealElement(final int i, final int j, final double x) {
188 realBuffer.put(i + nbRows * j, x);
195 public void setImaginaryElement(final int i, final int j, final double x) {
196 imaginaryBuffer.put(i + nbRows * j, x);
203 public void setElement(final int i, final int j, final double x, final double y) {
204 realBuffer.put(i + nbRows * j, x);
205 imaginaryBuffer.put(i + nbRows * j, x);
209 * When this object is deserialized we want a ScilabDouble, not a ScilabDoubleReference.
210 * @return a ScilabDouble
212 private Object readResolve() throws ObjectStreamException {
213 return new ScilabDouble(varName, realPart, imaginaryPart, swaped);
217 public int getHeight() {
222 public int getWidth() {