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 - Clément DAVID
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 package org.scilab.modules.types;
16 import java.util.Arrays;
19 * This class provides a representation on the Scilab Double datatype<br>
20 * Note that double is the default datatype in Scilab.<br>
22 * This class is {@link java.io.Serializable} and any modification could
23 * impact load and store of data (Xcos files, Javasci saved data, etc...).<br>
25 * Example (real):<br />
27 * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br />
28 * ScilabDouble aMatrix = new ScilabDouble(a);<br />
30 * Example (complex):<br />
32 * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br />
33 * double [][]aImg={{210.2, 220.0, 420.0, 390.0},{230.2, 240.0, 440.0, 400.0}};<br />
34 * ScilabDouble aMatrix = new ScilabDouble(a, aImg);
37 * @see org.scilab.modules.javasci.Scilab
39 public class ScilabDouble implements ScilabType {
41 private static final long serialVersionUID = 879624048944109684L;
42 private static final ScilabTypeEnum type = ScilabTypeEnum.sci_matrix;
44 private double[][] realPart;
45 private double[][] imaginaryPart;
50 public ScilabDouble() {
56 * Constructor with a unique value.
57 * @param data the unique value
59 public ScilabDouble(double data) {
60 realPart = new double[1][1];
61 realPart[0][0] = data;
66 * Constructor with a unique complex value.
68 * @param realData the real part
69 * @param imagData the complex part
71 public ScilabDouble(double realData, double imagData) {
72 realPart = new double[1][1];
73 realPart[0][0] = realData;
74 imaginaryPart = new double[1][1];
75 imaginaryPart[0][0] = imagData;
79 * Constructor with a matrix of real data.
81 * @param data the data
83 public ScilabDouble(double[][] data) {
89 * Constructor with a matrix of complex numbers
91 * @param realData the real part of the data
92 * @param imagData the imaginary part of the data
94 public ScilabDouble(double[][] realData, double[][] imagData) {
96 imaginaryPart = imagData;
100 * Return the type of Scilab
101 * @return the type of Scilab
105 public ScilabTypeEnum getType() {
110 * Check the emptiness of the associated data.
111 * @return true, if the associated data array is empty.
114 public boolean isEmpty() {
115 return (realPart == null && imaginaryPart == null);
119 * Check if the current data doesn't have an imaginary part.
121 * @return true, if the data are real only.
123 public boolean isReal() {
124 return (imaginaryPart == null);
128 * Get the real part of the data.
130 * @return the real part.
132 public double[][] getRealPart() {
137 * Set the real part of the data.
139 * @param realPart the real part.
141 public void setRealPart(double[][] realPart) {
142 this.realPart = realPart;
146 * Get the imaginary part of the data.
148 * @return the imaginary part.
150 public double[][] getImaginaryPart() {
151 return imaginaryPart;
155 * Set the imaginary part of the data.
157 * @param imaginaryPart the imaginary part.
159 public void setImaginaryPart(double[][] imaginaryPart) {
160 this.imaginaryPart = imaginaryPart;
165 * Get complex matrix as a serialized form
167 * @return the serialized matrix with complex values
169 public double[] getSerializedComplexMatrix() {
170 int size = this.getHeight()*this.getWidth();
171 double [] serializedComplexMatrix = new double[size*2];
172 for (int i = 0; i < this.getHeight(); i++) {
173 for (int j = 0; j < this.getWidth(); j++) {
174 serializedComplexMatrix[j * this.getHeight() + i] = realPart[i][j];
175 serializedComplexMatrix[size+j * this.getHeight() + i] = imaginaryPart[i][j];
179 return serializedComplexMatrix;
184 * @return the height of the data matrix
185 * @see org.scilab.modules.types.ScilabType#getHeight()
188 public int getHeight() {
192 return realPart.length;
196 * @return the width of the data matrix
197 * @see org.scilab.modules.types.ScilabType#getWidth()
200 public int getWidth() {
201 if (isEmpty() || realPart.length == 0) {
205 return realPart[0].length;
209 * @see org.scilab.modules.types.ScilabType#equals(Object)
212 public boolean equals(Object obj) {
213 if (obj instanceof ScilabDouble) {
214 ScilabDouble sciDouble = ((ScilabDouble)obj);
215 if (this.isReal() && sciDouble.isReal()) {
216 return Arrays.deepEquals(this.getRealPart(), sciDouble.getRealPart());
219 return Arrays.deepEquals(this.getRealPart(), sciDouble.getRealPart()) && Arrays.deepEquals(this.getImaginaryPart(), sciDouble.getImaginaryPart());
228 * Display the representation in the Scilab language of the type<br />
229 * Note that the representation can be copied/pasted straight into Scilab
231 * @return a Scilab-like String representation of the data.
232 * @see java.lang.Object#toString()
235 public String toString() {
236 StringBuilder result = new StringBuilder();
240 return result.toString();
244 for (int i = 0; i < getHeight(); ++i) {
245 for (int j = 0; j < getWidth(); ++j) {
247 result.append(getRealPart()[i][j]);
249 result.append(getRealPart()[i][j] + " + " + getImaginaryPart()[i][j] + " * %i");
251 if (j != getWidth() - 1) {
255 if (i != getHeight() - 1) {
256 result.append(" ; ");
261 return result.toString();