KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > MatrixSeries


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------
23  * MatrixSeries.java
24  * -----------------
25  * (C) Copyright 2003 by Barak Naveh and Contributors.
26  *
27  * Original Author: Barak Naveh;;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: MatrixSeries.java,v 1.3 2003/09/03 15:08:51 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
35  *
36  */

37 package org.jfree.data;
38
39 import java.io.Serializable JavaDoc;
40
41 /**
42  * Represents a dense matrix M[i,j] where each Mij item of the matrix has a
43  * value (default is 0).
44  *
45  * @author Barak Naveh
46  */

47 public class MatrixSeries extends Series implements Serializable JavaDoc {
48     
49     /** Series matrix values */
50     protected double[][] m_data;
51
52     /**
53      * Constructs a new matrix series.
54      *
55      * <p>
56      * By default, all matrix items are initialzed to 0.
57      * </p>
58      *
59      * @param name series name.
60      * @param rows the number of rows.
61      * @param columns the number of columns.
62      */

63     public MatrixSeries(String JavaDoc name, int rows, int columns) {
64         super(name);
65         m_data = new double[rows][columns];
66         zeroAll();
67     }
68
69     /**
70      * Returns the number of columns in this matrix series.
71      *
72      * @return the number of columns in this matrix series.
73      */

74     public int getColumnsCount() {
75         return m_data[0].length;
76     }
77
78
79     /**
80      * Return the matrix item at the specified index.
81      *
82      * @param itemIndex item index.
83      *
84      * @return matrix item at the specified index.
85      */

86     public Number JavaDoc getItem(int itemIndex) {
87         int i = getItemRow(itemIndex);
88         int j = getItemColumn(itemIndex);
89
90         Number JavaDoc n = new Double JavaDoc(get(i, j));
91
92         return n;
93     }
94
95
96     /**
97      * Returns the column of the specified item.
98      *
99      * @param itemIndex the index of the item.
100      *
101      * @return the column of the specified item.
102      */

103     public int getItemColumn(int itemIndex) {
104         //assert itemIndex >= 0 && itemIndex < getItemCount();
105

106         return itemIndex % getRowCount();
107     }
108
109
110     /**
111      * Returns the number of items in the series.
112      *
113      * @return The item count.
114      */

115     public int getItemCount() {
116         return getRowCount() * getColumnsCount();
117     }
118
119
120     /**
121      * Returns the row of the specified item.
122      *
123      * @param itemIndex the index of the item.
124      *
125      * @return the row of the specified item.
126      */

127     public int getItemRow(int itemIndex) {
128         //assert itemIndex >= 0 && itemIndex < getItemCount();
129

130         return itemIndex / getRowCount();
131     }
132
133
134     /**
135      * Returns the number of rows in this matrix series.
136      *
137      * @return the number of rows in this matrix series.
138      */

139     public int getRowCount() {
140         return m_data.length;
141     }
142
143
144     /**
145      * Returns the value of the specified item in this matrix series.
146      *
147      * @param i the row of the item.
148      * @param j the column of the item.
149      *
150      * @return the value of the specified item in this matrix series.
151      */

152     public double get(int i, int j) {
153         return m_data[i][j];
154     }
155
156
157     /**
158      * Updates the value of the specified item in this matrix series.
159      *
160      * @param i the row of the item.
161      * @param j the column of the item.
162      * @param mij the new value for the item.
163      */

164     public void update(int i, int j, double mij) {
165         m_data[i][j] = mij;
166
167         fireSeriesChanged();
168     }
169
170
171     /**
172      * Sets all matrix values to zero.
173      */

174     public void zeroAll() {
175         double[][] data = m_data;
176         int rows = getRowCount();
177         int columns = getColumnsCount();
178
179         for (int i = 0; i < rows; i++) {
180             for (int j = 0; j < columns; j++) {
181                 data[i][j] = 0.0;
182             }
183         }
184
185         fireSeriesChanged();
186     }
187 }
188
Popular Tags