001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------
028 * MatrixSeries.java
029 * -----------------
030 * (C) Copyright 2003-2020, by Barak Naveh and Contributors.
031 *
032 * Original Author:  Barak Naveh;
033 * Contributor(s):   David Gilbert;
034 *                   Zhitao Wang;
035 *
036 */
037
038package org.jfree.data.xy;
039
040import java.io.Serializable;
041
042import org.jfree.data.general.Series;
043
044/**
045 * Represents a dense matrix M[i,j] where each Mij item of the matrix has a
046 * value (default is 0).
047 */
048public class MatrixSeries<K extends Comparable<K>> extends Series<K> 
049        implements Serializable {
050
051    /** For serialization. */
052    private static final long serialVersionUID = 7934188527308315704L;
053
054    /** Series matrix values */
055    protected double[][] data;
056
057    /**
058     * Constructs a new matrix series.
059     * <p>
060     * By default, all matrix items are initialzed to 0.
061     * </p>
062     *
063     * @param name  series name ({@code null} not permitted).
064     * @param rows  the number of rows.
065     * @param columns  the number of columns.
066     */
067    public MatrixSeries(K name, int rows, int columns) {
068        super(name);
069        this.data = new double[rows][columns];
070        zeroAll();
071    }
072
073    /**
074     * Returns the number of columns in this matrix series.
075     *
076     * @return The number of columns in this matrix series.
077     */
078    public int getColumnsCount() {
079        return this.data[0].length;
080    }
081
082
083    /**
084     * Return the matrix item at the specified index.  Note that this method
085     * creates a new {@code double} instance every time it is called.
086     *
087     * @param itemIndex item index.
088     *
089     * @return The matrix item at the specified index.
090     *
091     * @see #get(int, int)
092     */
093    public Number getItem(int itemIndex) {
094        int i = getItemRow(itemIndex);
095        int j = getItemColumn(itemIndex);
096        return get(i, j);
097    }
098
099
100    /**
101     * Returns the column of the specified item.
102     *
103     * @param itemIndex the index of the item.
104     *
105     * @return The column of the specified item.
106     */
107    public int getItemColumn(int itemIndex) {
108        //assert itemIndex >= 0 && itemIndex < getItemCount();
109        return itemIndex % getColumnsCount();
110    }
111
112
113    /**
114     * Returns the number of items in the series.
115     *
116     * @return The item count.
117     */
118    @Override
119    public int getItemCount() {
120        return getRowCount() * getColumnsCount();
121    }
122
123
124    /**
125     * Returns the row of the specified item.
126     *
127     * @param itemIndex the index of the item.
128     *
129     * @return The row of the specified item.
130     */
131    public int getItemRow(int itemIndex) {
132        //assert itemIndex >= 0 && itemIndex < getItemCount();
133        return itemIndex / getColumnsCount();
134    }
135
136
137    /**
138     * Returns the number of rows in this matrix series.
139     *
140     * @return The number of rows in this matrix series.
141     */
142    public int getRowCount() {
143        return this.data.length;
144    }
145
146
147    /**
148     * Returns the value of the specified item in this matrix series.
149     *
150     * @param i the row of the item.
151     * @param j the column of the item.
152     *
153     * @return The value of the specified item in this matrix series.
154     *
155     * @see #getItem(int)
156     * @see #update(int, int, double)
157     */
158    public double get(int i, int j) {
159        return this.data[i][j];
160    }
161
162
163    /**
164     * Updates the value of the specified item in this matrix series.
165     *
166     * @param i the row of the item.
167     * @param j the column of the item.
168     * @param mij the new value for the item.
169     *
170     * @see #get(int, int)
171     */
172    public void update(int i, int j, double mij) {
173        this.data[i][j] = mij;
174        fireSeriesChanged();
175    }
176
177
178    /**
179     * Sets all matrix values to zero and sends a
180     * {@link org.jfree.data.general.SeriesChangeEvent} to all registered
181     * listeners.
182     */
183    public void zeroAll() {
184        int rows = getRowCount();
185        int columns = getColumnsCount();
186
187        for (int row = 0; row < rows; row++) {
188            for (int column = 0; column < columns; column++) {
189                this.data[row][column] = 0.0;
190            }
191        }
192        fireSeriesChanged();
193    }
194
195    /**
196     * Tests this object instance for equality with an arbitrary object.
197     *
198     * @param obj  the object ({@code null} permitted).
199     *
200     * @return A boolean.
201     */
202    @Override
203    public boolean equals(Object obj) {
204        if (obj == this) {
205            return true;
206        }
207        if (!(obj instanceof MatrixSeries)) {
208            return false;
209        }
210        MatrixSeries that = (MatrixSeries) obj;
211        if (!(getRowCount() == that.getRowCount())) {
212            return false;
213        }
214        if (!(getColumnsCount() == that.getColumnsCount())) {
215            return false;
216        }
217        for (int r = 0; r < getRowCount(); r++) {
218            for (int c = 0; c < getColumnsCount(); c++) {
219                if (get(r, c) != that.get(r, c)) {
220                    return false;
221                }
222            }
223        }
224        return super.equals(obj);
225    }
226
227}