KickJava   Java API By Example, From Geeks To Geeks.

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


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  * NormalizedMatrixSeries.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: NormalizedMatrixSeries.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  
38 package org.jfree.data;
39
40 /**
41  * Represents a dense normalized matrix M[i,j] where each Mij item of the
42  * matrix has a value (default is 0). When a matrix item is observed using
43  * <code>getItem</code> method, it is normalized, that is, divided by the
44  * total sum of all items. It can be also be scaled by setting a scale factor.
45  *
46  * @author Barak Naveh
47  *
48  * @since Jun 18, 2003
49  */

50 public class NormalizedMatrixSeries extends MatrixSeries {
51     
52     /** The default scale factor. */
53     public static final double DEFAULT_SCALE_FACTOR = 1.0;
54
55     /**
56      * A factor that multiplies each item in this series when observed using getItem method.
57      */

58     private double m_scaleFactor = DEFAULT_SCALE_FACTOR;
59
60     /** The sum of all items in this matrix */
61     private double m_totalSum;
62
63     /**
64      * Constructor for NormalizedMatrixSeries.
65      *
66      * @param name the series name.
67      * @param rows the number of rows.
68      * @param columns the number of columns.
69      */

70     public NormalizedMatrixSeries(String JavaDoc name, int rows, int columns) {
71         super(name, rows, columns);
72
73         /*
74          * we assum super is always initialized to all-zero matrix, so the
75          * total sum should be 0 upon initialization. However, we set it to
76          * Double.MIN_VALUE to get the same effect and yet avoid division by 0
77          * upon initialization.
78          */

79         m_totalSum = Double.MIN_VALUE;
80     }
81
82     /**
83      * Returns an item.
84      *
85      * @param itemIndex the index.
86      *
87      * @return The value.
88      *
89      * @see org.jfree.data.MatrixSeries#getItem(int)
90      */

91     public Number JavaDoc getItem(int itemIndex) {
92         int i = getItemRow(itemIndex);
93         int j = getItemColumn(itemIndex);
94
95         double mij = get(i, j) * m_scaleFactor;
96         Number JavaDoc n = new Double JavaDoc(mij / m_totalSum);
97
98         return n;
99     }
100
101     /**
102      * Sets the factor that multiplies each item in this series when observed
103      * using getItem mehtod.
104      *
105      * @param factor new factor to set.
106      *
107      * @see #DEFAULT_SCALE_FACTOR
108      */

109     public void setScaleFactor(double factor) {
110         m_scaleFactor = factor;
111     }
112
113
114     /**
115      * Returns the factor that multiplies each item in this series when
116      * observed using getItem mehtod.
117      *
118      * @return the factor
119      */

120     public double getScaleFactor() {
121         return m_scaleFactor;
122     }
123
124
125     /**
126      * @see org.jfree.data.MatrixSeries#update(int, int, double)
127      */

128     public void update(int i, int j, double mij) {
129         m_totalSum -= get(i, j);
130         m_totalSum += mij;
131
132         super.update(i, j, mij);
133     }
134
135
136     /**
137      * @see org.jfree.data.MatrixSeries#zeroAll()
138      */

139     public void zeroAll() {
140         m_totalSum = 0;
141         super.zeroAll();
142     }
143 }
144
Popular Tags