Fix some minor bugs, Update of the comments, 100 % test coverage, tests update and...
[scilab.git] / scilab / modules / types / src / java / org / scilab / modules / types / ScilabDouble.java
1 /*
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
5  * 
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
11  * 
12  */
13
14 package org.scilab.modules.types;
15
16 import java.util.Arrays;
17
18 /**
19  * This class provides a wrapping on the Scilab Double datatype<br />
20  * Note that double is the default datatype in Scilab
21  */
22 public class ScilabDouble implements ScilabType {
23
24         private double[][] realPart;
25         private double[][] imaginaryPart;
26
27         /**
28          * Default constructor
29          */
30         public ScilabDouble() {
31                 realPart = null;
32                 imaginaryPart = null;
33         }
34
35         /**
36          * Constructor with a unique value.
37          * @param data the unique value
38          */
39         public ScilabDouble(double data) {
40                 realPart = new double[1][1];
41                 realPart[0][0] = data;
42                 imaginaryPart = null;
43         }
44
45         /**
46          * Constructor with a unique complex value.
47          * 
48          * @param realData the real part
49          * @param imagData the complex part 
50          */
51         public ScilabDouble(double realData, double imagData) {
52                 realPart = new double[1][1];
53                 realPart[0][0] = realData;
54                 imaginaryPart = new double[1][1];
55                 imaginaryPart[0][0] = imagData;
56         }
57
58         /**
59          * Constructor with a matrix of real data.
60          * 
61          * @param data the data
62          */
63         public ScilabDouble(double[][] data) {
64                 realPart = data;
65                 imaginaryPart = null;
66         }
67
68         /**
69          * Constructor with a matrix of complex numbers 
70          * 
71          * @param realData the real part of the data
72          * @param imagData the imaginary part of the data
73          */
74         public ScilabDouble(double[][] realData, double[][] imagData) {
75                 realPart = realData;
76                 imaginaryPart = imagData;
77         }
78
79         /**
80          * Check the emptiness of the associated data.
81          * @return true, if the associated data array is empty. 
82          */
83         public boolean isEmpty() {
84                 return (realPart == null && imaginaryPart == null);
85         }
86
87         /**
88          * Check if the current data doesn't have an imaginary part.
89          * 
90          * @return true, if the data are real only.
91          */
92         public boolean isReal() {
93                 return (imaginaryPart == null);
94         }
95
96         /**
97          * Get the real part of the data.
98          * 
99          * @return the real part.
100          */
101         public double[][] getRealPart() {
102                 return realPart;
103         }
104
105         /**
106          * Set the real part of the data.
107          * 
108          * @param realPart the real part.
109          */
110         public void setRealPart(double[][] realPart) {
111                 this.realPart = realPart;
112         }
113
114         /**
115          * Get the imaginary part of the data.
116          * 
117          * @return the imaginary part.
118          */
119         public double[][] getImaginaryPart() {
120                 return imaginaryPart;
121         }
122
123         /**
124          * Set the imaginary part of the data.
125          * 
126          * @param imaginaryPart the imaginary part.
127          */
128         public void setImaginaryPart(double[][] imaginaryPart) {
129                 this.imaginaryPart = imaginaryPart;
130         }
131
132
133         /**
134          * Get complex matrix as a serialized form
135          * 
136          * @return the serialized matrix with complex values
137          */
138         public double[] getSerializedComplexMatrix() {
139                 int size = this.getHeight()*this.getWidth();
140                 double [] serializedComplexMatrix = new double[size*2];
141                 for (int i = 0; i < this.getHeight(); i++) {
142                         for (int j = 0; j < this.getWidth(); j++) {
143                                 serializedComplexMatrix[j * this.getHeight() + i] = realPart[i][j];
144                                 serializedComplexMatrix[size+j * this.getHeight() + i] = imaginaryPart[i][j];
145                         }
146                 }
147
148                 return serializedComplexMatrix;
149         }
150
151
152         /**
153          * @return the height of the data matrix
154          * @see org.scilab.modules.types.ScilabType#getHeight()
155          */
156         @Override
157         public int getHeight() {
158                 if (isEmpty()) {
159                         return 0;
160                 }
161                 return realPart.length;
162         }
163
164         /**
165          * @return the width of the data matrix
166          * @see org.scilab.modules.types.ScilabType#getWidth()
167          */
168         @Override
169         public int getWidth() {
170                 if (isEmpty() || realPart.length  == 0) {
171                         return 0;
172                 }
173                 
174                 return realPart[0].length;
175         }
176
177         /**
178          * @see org.scilab.modules.types.ScilabType#equals(Object)
179          */
180         public boolean equals(Object obj) {
181                 if (obj instanceof ScilabDouble) {
182                         ScilabDouble sciDouble = ((ScilabDouble)obj);
183                         if (this.isReal() && sciDouble.isReal()) {
184                                 return Arrays.deepEquals(this.getRealPart(), sciDouble.getRealPart());
185                         } else {
186                                 /* Complex */
187                                 return Arrays.deepEquals(this.getRealPart(), sciDouble.getRealPart()) && Arrays.deepEquals(this.getImaginaryPart(), sciDouble.getImaginaryPart());
188                         }
189                 } else {
190                         return false;
191                 }
192         }
193
194
195         /**
196          * Display the representation in the Scilab language of the type
197          * @return a Scilab-like String representation of the data.
198          * @see java.lang.Object#toString()
199          */
200         @Override
201         public String toString() {
202                 StringBuilder result = new StringBuilder();
203                 
204                 if (isEmpty()) {
205                         result.append("[]");
206                         return result.toString();
207                 }
208
209                 result.append("[");
210                 for (int i = 0; i < getHeight(); ++i) {
211                         for (int j = 0; j < getWidth(); ++j) {
212                                 if (isReal()) {
213                                         result.append(getRealPart()[i][j]);
214                                 } else {
215                                         result.append(getRealPart()[i][j] + " + " + getImaginaryPart()[i][j] + " * %i");
216                                 }
217                                 if (j != getWidth() - 1) {
218                                         result.append(", ");
219                                 }
220                         }
221                         if (i != getHeight() - 1) {
222                                 result.append(" ; ");
223                         }
224                 }
225                 result.append("]");
226                 
227                 return result.toString();
228         }
229
230 }