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
22 * Example (real):<br />
24 * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br />
25 * ScilabDouble aMatrix = new ScilabDouble(a);<br />
28 * Example (complex):<br />
30 * double [][]a={{21.2, 22.0, 42.0, 39.0},{23.2, 24.0, 44.0, 40.0}};<br />
31 * double [][]aImg={{210.2, 220.0, 420.0, 390.0},{230.2, 240.0, 440.0, 400.0}};<br />
33 * ScilabDouble aMatrix = new ScilabDouble(a, aImg);
35 * @see org.scilab.modules.javasci.Scilab
37 public class ScilabDouble implements ScilabType {
39 private static final long serialVersionUID = 879624048944109684L;
40 private double[][] realPart;
41 private double[][] imaginaryPart;
46 public ScilabDouble() {
52 * Constructor with a unique value.
53 * @param data the unique value
55 public ScilabDouble(double data) {
56 realPart = new double[1][1];
57 realPart[0][0] = data;
62 * Constructor with a unique complex value.
64 * @param realData the real part
65 * @param imagData the complex part
67 public ScilabDouble(double realData, double imagData) {
68 realPart = new double[1][1];
69 realPart[0][0] = realData;
70 imaginaryPart = new double[1][1];
71 imaginaryPart[0][0] = imagData;
75 * Constructor with a matrix of real data.
77 * @param data the data
79 public ScilabDouble(double[][] data) {
85 * Constructor with a matrix of complex numbers
87 * @param realData the real part of the data
88 * @param imagData the imaginary part of the data
90 public ScilabDouble(double[][] realData, double[][] imagData) {
92 imaginaryPart = imagData;
96 * Check the emptiness of the associated data.
97 * @return true, if the associated data array is empty.
99 public boolean isEmpty() {
100 return (realPart == null && imaginaryPart == null);
104 * Check if the current data doesn't have an imaginary part.
106 * @return true, if the data are real only.
108 public boolean isReal() {
109 return (imaginaryPart == null);
113 * Get the real part of the data.
115 * @return the real part.
117 public double[][] getRealPart() {
122 * Set the real part of the data.
124 * @param realPart the real part.
126 public void setRealPart(double[][] realPart) {
127 this.realPart = realPart;
131 * Get the imaginary part of the data.
133 * @return the imaginary part.
135 public double[][] getImaginaryPart() {
136 return imaginaryPart;
140 * Set the imaginary part of the data.
142 * @param imaginaryPart the imaginary part.
144 public void setImaginaryPart(double[][] imaginaryPart) {
145 this.imaginaryPart = imaginaryPart;
150 * Get complex matrix as a serialized form
152 * @return the serialized matrix with complex values
154 public double[] getSerializedComplexMatrix() {
155 int size = this.getHeight()*this.getWidth();
156 double [] serializedComplexMatrix = new double[size*2];
157 for (int i = 0; i < this.getHeight(); i++) {
158 for (int j = 0; j < this.getWidth(); j++) {
159 serializedComplexMatrix[j * this.getHeight() + i] = realPart[i][j];
160 serializedComplexMatrix[size+j * this.getHeight() + i] = imaginaryPart[i][j];
164 return serializedComplexMatrix;
169 * @return the height of the data matrix
170 * @see org.scilab.modules.types.ScilabType#getHeight()
173 public int getHeight() {
177 return realPart.length;
181 * @return the width of the data matrix
182 * @see org.scilab.modules.types.ScilabType#getWidth()
185 public int getWidth() {
186 if (isEmpty() || realPart.length == 0) {
190 return realPart[0].length;
194 * @see org.scilab.modules.types.ScilabType#equals(Object)
196 public boolean equals(Object obj) {
197 if (obj instanceof ScilabDouble) {
198 ScilabDouble sciDouble = ((ScilabDouble)obj);
199 if (this.isReal() && sciDouble.isReal()) {
200 return Arrays.deepEquals(this.getRealPart(), sciDouble.getRealPart());
203 return Arrays.deepEquals(this.getRealPart(), sciDouble.getRealPart()) && Arrays.deepEquals(this.getImaginaryPart(), sciDouble.getImaginaryPart());
212 * Display the representation in the Scilab language of the type<br />
213 * Note that the representation can be copied/pasted straight into Scilab
215 * @return a Scilab-like String representation of the data.
216 * @see java.lang.Object#toString()
219 public String toString() {
220 StringBuilder result = new StringBuilder();
224 return result.toString();
228 for (int i = 0; i < getHeight(); ++i) {
229 for (int j = 0; j < getWidth(); ++j) {
231 result.append(getRealPart()[i][j]);
233 result.append(getRealPart()[i][j] + " + " + getImaginaryPart()[i][j] + " * %i");
235 if (j != getWidth() - 1) {
239 if (i != getHeight() - 1) {
240 result.append(" ; ");
245 return result.toString();