001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, by Object Refinery Limited 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------------------
028 * NormalizedMatrixSeries.java
029 * ---------------------------
030 * (C) Copyright 2003-2008, by Barak Naveh and Contributors.
031 *
032 * Original Author:  Barak Naveh;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes
036 * -------
037 * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
038 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
039 *
040 */
041
042package org.jfree.data.xy;
043
044/**
045 * Represents a dense normalized matrix M[i,j] where each Mij item of the
046 * matrix has a value (default is 0). When a matrix item is observed using
047 * <code>getItem</code> method, it is normalized, that is, divided by the
048 * total sum of all items. It can be also be scaled by setting a scale factor.
049 */
050public class NormalizedMatrixSeries extends MatrixSeries {
051
052    /** The default scale factor. */
053    public static final double DEFAULT_SCALE_FACTOR = 1.0;
054
055    /**
056     * A factor that multiplies each item in this series when observed using
057     * getItem method.
058     */
059    private double m_scaleFactor = DEFAULT_SCALE_FACTOR;
060
061    /** The sum of all items in this matrix */
062    private double m_totalSum;
063
064    /**
065     * Constructor for NormalizedMatrixSeries.
066     *
067     * @param name  the series name.
068     * @param rows  the number of rows.
069     * @param columns  the number of columns.
070     */
071    public NormalizedMatrixSeries(String name, int rows, int columns) {
072        super(name, rows, columns);
073
074        /*
075         * we assum super is always initialized to all-zero matrix, so the
076         * total sum should be 0 upon initialization. However, we set it to
077         * Double.MIN_VALUE to get the same effect and yet avoid division by 0
078         * upon initialization.
079         */
080        this.m_totalSum = Double.MIN_VALUE;
081    }
082
083    /**
084     * Returns an item.
085     *
086     * @param itemIndex  the index.
087     *
088     * @return The value.
089     *
090     * @see org.jfree.data.xy.MatrixSeries#getItem(int)
091     */
092    public Number getItem(int itemIndex) {
093        int i = getItemRow(itemIndex);
094        int j = getItemColumn(itemIndex);
095
096        double mij = get(i, j) * this.m_scaleFactor;
097        Number n = new Double(mij / this.m_totalSum);
098
099        return n;
100    }
101
102    /**
103     * Sets the factor that multiplies each item in this series when observed
104     * using getItem mehtod.
105     *
106     * @param factor new factor to set.
107     *
108     * @see #DEFAULT_SCALE_FACTOR
109     */
110    public void setScaleFactor(double factor) {
111        this.m_scaleFactor = factor;
112        // FIXME: this should generate a series change event
113    }
114
115
116    /**
117     * Returns the factor that multiplies each item in this series when
118     * observed using getItem mehtod.
119     *
120     * @return The factor
121     */
122    public double getScaleFactor() {
123        return this.m_scaleFactor;
124    }
125
126
127    /**
128     * Updates the value of the specified item in this matrix series.
129     *
130     * @param i the row of the item.
131     * @param j the column of the item.
132     * @param mij the new value for the item.
133     *
134     * @see #get(int, int)
135     */
136    public void update(int i, int j, double mij) {
137        this.m_totalSum -= get(i, j);
138        this.m_totalSum += mij;
139
140        super.update(i, j, mij);
141    }
142
143    /**
144     * @see org.jfree.data.xy.MatrixSeries#zeroAll()
145     */
146    public void zeroAll() {
147        this.m_totalSum = 0;
148        super.zeroAll();
149    }
150}