2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011-2011 - DIGITEO - Calixte DENIZET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 package org.scilab.modules.types;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.util.Arrays;
24 * This class provides a representation on the Scilab Polynomial datatype<br>
26 * This class is {@link java.io.Serializable} and any modification could impact
27 * load and store of data (Xcos files, Javasci saved data, etc...).<br>
29 * TODO Sly: faire de la doc
31 * @see org.scilab.modules.javasci.Scilab
33 public class ScilabPolynomial implements ScilabType {
35 private static final long serialVersionUID = 870624048944109684L;
36 private static final ScilabTypeEnum type = ScilabTypeEnum.sci_poly;
38 private static final int VERSION = 0;
40 private double[][][] realPart;
41 private double[][][] imaginaryPart;
43 private String varName;
44 private String polyVarName = "x";
45 private boolean swaped;
50 public ScilabPolynomial() {
56 * Constructor with a unique polynomial.
59 * the unique polynomial
61 public ScilabPolynomial(double[] data) {
62 realPart = new double[1][1][];
63 realPart[0][0] = data;
68 * Constructor with a unique polynomial.
71 * the unique polynomial
73 public ScilabPolynomial(double[] data, String polyVarName) {
75 setPolyVarName(polyVarName);
79 * Constructor with a unique complex polynomial.
86 public ScilabPolynomial(double[] realData, double[] imagData) {
87 realPart = new double[1][1][];
88 realPart[0][0] = realData;
89 imaginaryPart = new double[1][1][];
90 imaginaryPart[0][0] = imagData;
94 * Constructor with a unique complex polynomial.
101 public ScilabPolynomial(double[] realData, double[] imagData, String polyVarName) {
102 this(realData, imagData);
103 setPolyVarName(polyVarName);
107 * Constructor with a matrix of real polynomials.
112 public ScilabPolynomial(double[][][] data) {
114 imaginaryPart = null;
118 * Constructor with a matrix of real polynomials.
123 public ScilabPolynomial(double[][][] data, String polyVarName) {
125 setPolyVarName(polyVarName);
129 * Constructor with a matrix of complex polynomials
132 * the real part of the data
134 * the imaginary part of the data
136 public ScilabPolynomial(double[][][] realData, double[][][] imagData) {
138 imaginaryPart = imagData;
142 * Constructor with a matrix of complex polynomials
145 * the real part of the data
147 * the imaginary part of the data
149 public ScilabPolynomial(double[][][] realData, double[][][] imagData, String polyVarName) {
150 this(realData, imagData);
151 setPolyVarName(polyVarName);
155 * Constructor with a matrix of complex polynomials
160 * the polynomial variable name
162 * the real part of the data
164 * the imaginary part of the data
166 * if true the matrix is stored row by row
168 public ScilabPolynomial(String varName, String polyVarName, double[][][] realData, double[][][] imagData, boolean swaped) {
169 this.varName = varName;
170 this.polyVarName = polyVarName;
171 this.swaped = swaped;
173 imaginaryPart = imagData;
179 public boolean isReference() {
184 * Return the type of Scilab
186 * @return the type of Scilab
190 public ScilabTypeEnum getType() {
195 * Check the emptiness of the associated data.
197 * @return true, if the associated data array is empty.
200 public boolean isEmpty() {
201 return (realPart == null && imaginaryPart == null);
205 * Check if the current data doesn't have an imaginary part.
207 * @return true, if the data are real only.
209 public boolean isReal() {
210 return (imaginaryPart == null);
214 * Get the real part of the data.
216 * @return the real part.
218 public double[][][] getRealPart() {
223 * Set the real part of the data.
228 public void setRealPart(double[][][] realPart) {
229 this.realPart = realPart;
233 * Get the imaginary part of the data.
235 * @return the imaginary part.
237 public double[][][] getImaginaryPart() {
238 return imaginaryPart;
242 * Set the imaginary part of the data.
244 * @param imaginaryPart
245 * the imaginary part.
247 public void setImaginaryPart(double[][][] imaginaryPart) {
248 this.imaginaryPart = imaginaryPart;
252 * Get the polynomila variable name
256 public String getPolyVarName() {
260 public void setPolyVarName(String polyVarName) {
261 this.polyVarName = polyVarName;
267 public String getVarName() {
274 public boolean isSwaped() {
279 * Get complex matrix as a serialized form
281 * @return the serialized matrix with complex values
283 // TODO Sly : serializer ce machin
284 public double[][] getSerializedComplexPolynomial() {
285 return new double[0][0];
289 * @return the height of the data matrix
290 * @see org.scilab.modules.types.ScilabType#getHeight()
293 public int getHeight() {
297 return realPart.length;
301 * @return the width of the data matrix
302 * @see org.scilab.modules.types.ScilabType#getWidth()
305 public int getWidth() {
306 if (isEmpty() || realPart.length == 0) {
310 return realPart[0].length;
315 public int hashCode() {
316 final int prime = 31;
318 result = prime * result + Arrays.deepHashCode(imaginaryPart);
319 result = prime * result + ((polyVarName == null) ? 0 : polyVarName.hashCode());
320 result = prime * result + Arrays.deepHashCode(realPart);
325 * @see org.scilab.modules.types.ScilabType#equals(Object)
328 public boolean equals(Object obj) {
329 if (obj instanceof ScilabPolynomial) {
330 ScilabPolynomial sciPoly = ((ScilabPolynomial) obj);
331 if (sciPoly.getPolyVarName().equals(getPolyVarName())) {
332 if (this.isReal() && sciPoly.isReal()) {
333 return Arrays.deepEquals(this.getRealPart(), sciPoly.getRealPart());
336 return Arrays.deepEquals(this.getRealPart(), sciPoly.getRealPart())
337 && Arrays.deepEquals(this.getImaginaryPart(), sciPoly.getImaginaryPart());
350 public Object getSerializedObject() {
352 return new Object[] { polyVarName, realPart };
354 return new Object[] { polyVarName, realPart, imaginaryPart };
359 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
360 int version = in.readInt();
363 realPart = (double[][][]) in.readObject();
364 imaginaryPart = (double[][][]) in.readObject();
365 varName = (String) in.readObject();
366 polyVarName = in.readUTF();
367 swaped = in.readBoolean();
370 throw new ClassNotFoundException("A class ScilabPolynomial with a version " + version + " does not exists");
375 public void writeExternal(ObjectOutput out) throws IOException {
376 out.writeInt(VERSION);
377 out.writeObject(realPart);
378 out.writeObject(imaginaryPart);
379 out.writeObject(varName);
380 out.writeUTF(polyVarName);
381 out.writeBoolean(swaped);
385 * Display the representation in the Scilab language of the type<BR>
386 * Note that the representation can be copied/pasted straight into Scilab
388 * @return a Scilab-like String representation of the data.
389 * @see java.lang.Object#toString()
392 public String toString() {
393 StringBuilder result = new StringBuilder();
397 return result.toString();
402 for (int i = 0; i < getHeight(); ++i) {
403 for (int j = 0; j < getWidth(); ++j) {
404 result.append("poly([");
405 for (int k = 0; k < realPart[i][j].length; k++) {
407 result.append(Double.toString(realPart[i][j][k]));
409 result.append(Double.toString(realPart[i][j][k]));
410 if (imaginaryPart[i][j][k] != 0) {
412 result.append(Double.toString(imaginaryPart[i][j][k]));
413 result.append("*%i");
416 if (k < realPart[i][j].length - 1) {
420 result.append("], \"");
421 result.append(getPolyVarName());
422 result.append("\", \"coeff\")");
424 if (j < getWidth() - 1) {
428 if (i < getHeight() - 1) {
429 result.append(" ; ");
434 return result.toString();