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