View Javadoc
1   /*
2    * Copyright 2007, 2008, 2009 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.type;
17  
18  import eu.simuline.octave.type.matrix.AbstractGenericMatrix;
19  
20  import eu.simuline.octave.util.StringUtil;
21  
22  import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
23  
24  /**
25   * Represents a matrix of doubles. 
26   */
27  public final class OctaveDouble 
28      extends AbstractGenericMatrix<double[], DoubleArrayList> {
29  
30      /**
31       * Create new matrix. 
32       * 
33       * @param size
34       */
35      // used by OctaveComplex, Octave, OctaveDouble, OctaveCell, 
36      // Reader and Writer
37      public OctaveDouble(final int... size) {
38          super(size);
39      }
40  
41      /**
42       * Constructor that reuses the input data. 
43       * 
44       * @param data
45       * @param size
46       */
47      // used by the end user only 
48      public OctaveDouble(final double[] data, final int... size) {
49          super(data, size);
50      }
51  
52      /**
53       * Copy constructor. 
54       * 
55       * @param o
56       */
57      // used by OctaveComplex, OctaveDouble
58      protected OctaveDoubleaveDouble.html#OctaveDouble">OctaveDouble(final OctaveDouble o) {
59          super(o);
60      }
61  
62      protected final DoubleArrayList newL(final int size) {
63  	DoubleArrayList list = new DoubleArrayList(size);
64  	list.size(size);
65  	return list;
66      }
67  
68      protected final int initL(double[] data, final int size) {
69  	this.dataL = new DoubleArrayList(data);
70  	this.dataL.size(size);
71  	return data.length;
72      }
73  
74      protected double[] getDataA() {
75  	return this.dataL.elements();
76      }
77  
78  
79      /**
80       * Set the value resizing by need. 
81       * 
82       * @param value
83       * @param pos
84       * @see #setPlain(double, int)
85       */
86      public final void set(final double value, final int... pos) {
87          resizeUp(pos);
88          setPlain(value, pos2ind(pos));
89      }
90  
91      /**
92       * Set the value assuming resize is not necessary. 
93       * 
94       * @param value
95       * @param pos
96       * @see #set(double, int[])
97       */
98      public final void setPlain(final double value, final int pos) {
99  	this.dataL.set(pos, value);
100     }
101 
102     // api-docs inherited from AbstractGenericMatrix 
103     public final void setPlain(final String value, final int pos) {
104 	this.dataL.set(pos, StringUtil.parseDouble(value));
105     }
106 
107     /**
108      * Get the value. 
109      * 
110      * @param pos
111      * @return value at pos
112      */
113     public final double get(final int... pos) {
114 	return this.dataL.getDouble(pos2ind(pos));
115     }
116 
117     public final String getPlainString(int pos) {
118 	return Double.toString(this.dataL.getDouble(pos));
119     }
120 
121     // **** needed at least for OctaveComplex, 
122     public OctaveDouble zero() {
123 	return new OctaveDouble(this.size);
124     }
125 
126     // api-docs inherited from OctaveObject
127     @Override
128     public OctaveDouble shallowCopy() {
129         return new OctaveDouble(this);
130     }
131 
132 }