/* ===========================================================_x000D_
 * JFreeChart : a free chart library for the Java(tm) platform_x000D_
 * ===========================================================_x000D_
 *_x000D_
 * (C) Copyright 2000-2022, by David Gilbert and Contributors._x000D_
 *_x000D_
 * Project Info:  http://www.jfree.org/jfreechart/index.html_x000D_
 *_x000D_
 * This library is free software; you can redistribute it and/or modify it_x000D_
 * under the terms of the GNU Lesser General Public License as published by_x000D_
 * the Free Software Foundation; either version 2.1 of the License, or_x000D_
 * (at your option) any later version._x000D_
 *_x000D_
 * This library is distributed in the hope that it will be useful, but_x000D_
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY_x000D_
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public_x000D_
 * License for more details._x000D_
 *_x000D_
 * You should have received a copy of the GNU Lesser General Public_x000D_
 * License along with this library; if not, write to the Free Software_x000D_
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,_x000D_
 * USA._x000D_
 *_x000D_
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. _x000D_
 * Other names may be trademarks of their respective owners.]_x000D_
 *_x000D_
 * ---------------------------_x000D_
 * NormalizedMatrixSeries.java_x000D_
 * ---------------------------_x000D_
 * (C) Copyright 2003-2020, by Barak Naveh and Contributors._x000D_
 *_x000D_
 * Original Author:  Barak Naveh;_x000D_
 * Contributor(s):   David Gilbert;_x000D_
 *_x000D_
 */_x000D_
_x000D_
package org.jfree.data.xy;_x000D_
_x000D_
/**_x000D_
 * Represents a dense normalized matrix M[i,j] where each Mij item of the_x000D_
 * matrix has a value (default is 0). When a matrix item is observed using_x000D_
 * {@code getItem()} method, it is normalized, that is, divided by the_x000D_
 * total sum of all items. It can be also be scaled by setting a scale factor._x000D_
 */_x000D_
public class NormalizedMatrixSeries extends MatrixSeries {_x000D_
_x000D_
    /** The default scale factor. */_x000D_
    public static final double DEFAULT_SCALE_FACTOR = 1.0;_x000D_
_x000D_
    /**_x000D_
     * A factor that multiplies each item in this series when observed using_x000D_
     * getItem method._x000D_
     */_x000D_
    private double m_scaleFactor = DEFAULT_SCALE_FACTOR;_x000D_
_x000D_
    /** The sum of all items in this matrix */_x000D_
    private double m_totalSum;_x000D_
_x000D_
    /**_x000D_
     * Constructor for NormalizedMatrixSeries._x000D_
     *_x000D_
     * @param name  the series name._x000D_
     * @param rows  the number of rows._x000D_
     * @param columns  the number of columns._x000D_
     */_x000D_
    public NormalizedMatrixSeries(String name, int rows, int columns) {_x000D_
        super(name, rows, columns);_x000D_
_x000D_
        /*_x000D_
         * we assum super is always initialized to all-zero matrix, so the_x000D_
         * total sum should be 0 upon initialization. However, we set it to_x000D_
         * Double.MIN_VALUE to get the same effect and yet avoid division by 0_x000D_
         * upon initialization._x000D_
         */_x000D_
        this.m_totalSum = Double.MIN_VALUE;_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * Returns an item._x000D_
     *_x000D_
     * @param itemIndex  the index._x000D_
     *_x000D_
     * @return The value._x000D_
     *_x000D_
     * @see org.jfree.data.xy.MatrixSeries#getItem(int)_x000D_
     */_x000D_
    @Override_x000D_
    public Number getItem(int itemIndex) {_x000D_
        int i = getItemRow(itemIndex);_x000D_
        int j = getItemColumn(itemIndex);_x000D_
_x000D_
        double mij = get(i, j) * this.m_scaleFactor;_x000D_
        Number n = mij / this.m_totalSum;_x000D_
_x000D_
        return n;_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * Sets the factor that multiplies each item in this series when observed_x000D_
     * using getItem mehtod._x000D_
     *_x000D_
     * @param factor new factor to set._x000D_
     *_x000D_
     * @see #DEFAULT_SCALE_FACTOR_x000D_
     */_x000D_
    public void setScaleFactor(double factor) {_x000D_
        this.m_scaleFactor = factor;_x000D_
        // FIXME: this should generate a series change event_x000D_
    }_x000D_
_x000D_
_x000D_
    /**_x000D_
     * Returns the factor that multiplies each item in this series when_x000D_
     * observed using getItem mehtod._x000D_
     *_x000D_
     * @return The factor_x000D_
     */_x000D_
    public double getScaleFactor() {_x000D_
        return this.m_scaleFactor;_x000D_
    }_x000D_
_x000D_
_x000D_
    /**_x000D_
     * Updates the value of the specified item in this matrix series._x000D_
     *_x000D_
     * @param i the row of the item._x000D_
     * @param j the column of the item._x000D_
     * @param mij the new value for the item._x000D_
     *_x000D_
     * @see #get(int, int)_x000D_
     */_x000D_
    @Override_x000D_
    public void update(int i, int j, double mij) {_x000D_
        this.m_totalSum -= get(i, j);_x000D_
        this.m_totalSum += mij;_x000D_
_x000D_
        super.update(i, j, mij);_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * @see org.jfree.data.xy.MatrixSeries#zeroAll()_x000D_
     */_x000D_
    @Override_x000D_
    public void zeroAll() {_x000D_
        this.m_totalSum = 0;_x000D_
        super.zeroAll();_x000D_
    }_x000D_
}_x000D_

/**_x000D_
     * @see org.jfree.data.xy.MatrixSeries#zeroAll()_x000D_
     */
/* ===========================================================_x000D_
 * JFreeChart : a free chart library for the Java(tm) platform_x000D_
 * ===========================================================_x000D_
 *_x000D_
 * (C) Copyright 2000-2022, by David Gilbert and Contributors._x000D_
 *_x000D_
 * Project Info:  http://www.jfree.org/jfreechart/index.html_x000D_
 *_x000D_
 * This library is free software; you can redistribute it and/or modify it_x000D_
 * under the terms of the GNU Lesser General Public License as published by_x000D_
 * the Free Software Foundation; either version 2.1 of the License, or_x000D_
 * (at your option) any later version._x000D_
 *_x000D_
 * This library is distributed in the hope that it will be useful, but_x000D_
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY_x000D_
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public_x000D_
 * License for more details._x000D_
 *_x000D_
 * You should have received a copy of the GNU Lesser General Public_x000D_
 * License along with this library; if not, write to the Free Software_x000D_
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,_x000D_
 * USA._x000D_
 *_x000D_
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. _x000D_
 * Other names may be trademarks of their respective owners.]_x000D_
 *_x000D_
 * ---------------------------_x000D_
 * NormalizedMatrixSeries.java_x000D_
 * ---------------------------_x000D_
 * (C) Copyright 2003-2020, by Barak Naveh and Contributors._x000D_
 *_x000D_
 * Original Author:  Barak Naveh;_x000D_
 * Contributor(s):   David Gilbert;_x000D_
 *_x000D_
 */_x000D_
_x000D_
package org.jfree.data.xy;_x000D_
_x000D_
/**_x000D_
 * Represents a dense normalized matrix M[i,j] where each Mij item of the_x000D_
 * matrix has a value (default is 0). When a matrix item is observed using_x000D_
 * {@code getItem()} method, it is normalized, that is, divided by the_x000D_
 * total sum of all items. It can be also be scaled by setting a scale factor._x000D_
 */_x000D_
public class NormalizedMatrixSeries extends MatrixSeries {_x000D_
_x000D_
    /** The default scale factor. */_x000D_
    public static final double DEFAULT_SCALE_FACTOR = 1.0;_x000D_
_x000D_
    /**_x000D_
     * A factor that multiplies each item in this series when observed using_x000D_
     * getItem method._x000D_
     */_x000D_
    private double m_scaleFactor = DEFAULT_SCALE_FACTOR;_x000D_
_x000D_
    /** The sum of all items in this matrix */_x000D_
    private double m_totalSum;_x000D_
_x000D_
    /**_x000D_
     * Constructor for NormalizedMatrixSeries._x000D_
     *_x000D_
     * @param name  the series name._x000D_
     * @param rows  the number of rows._x000D_
     * @param columns  the number of columns._x000D_
     */_x000D_
    public NormalizedMatrixSeries(String name, int rows, int columns) {_x000D_
        super(name, rows, columns);_x000D_
_x000D_
        /*_x000D_
         * we assum super is always initialized to all-zero matrix, so the_x000D_
         * total sum should be 0 upon initialization. However, we set it to_x000D_
         * Double.MIN_VALUE to get the same effect and yet avoid division by 0_x000D_
         * upon initialization._x000D_
         */_x000D_
        this.m_totalSum = Double.MIN_VALUE;_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * Returns an item._x000D_
     *_x000D_
     * @param itemIndex  the index._x000D_
     *_x000D_
     * @return The value._x000D_
     *_x000D_
     * @see org.jfree.data.xy.MatrixSeries#getItem(int)_x000D_
     */_x000D_
    @Override_x000D_
    public Number getItem(int itemIndex) {_x000D_
        int i = getItemRow(itemIndex);_x000D_
        int j = getItemColumn(itemIndex);_x000D_
_x000D_
        double mij = get(i, j) * this.m_scaleFactor;_x000D_
        Number n = mij / this.m_totalSum;_x000D_
_x000D_
        return n;_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * Sets the factor that multiplies each item in this series when observed_x000D_
     * using getItem mehtod._x000D_
     *_x000D_
     * @param factor new factor to set._x000D_
     *_x000D_
     * @see #DEFAULT_SCALE_FACTOR_x000D_
     */_x000D_
    public void setScaleFactor(double factor) {_x000D_
        this.m_scaleFactor = factor;_x000D_
        // FIXME: this should generate a series change event_x000D_
    }_x000D_
_x000D_
_x000D_
    /**_x000D_
     * Returns the factor that multiplies each item in this series when_x000D_
     * observed using getItem mehtod._x000D_
     *_x000D_
     * @return The factor_x000D_
     */_x000D_
    public double getScaleFactor() {_x000D_
        return this.m_scaleFactor;_x000D_
    }_x000D_
_x000D_
_x000D_
    /**_x000D_
     * Updates the value of the specified item in this matrix series._x000D_
     *_x000D_
     * @param i the row of the item._x000D_
     * @param j the column of the item._x000D_
     * @param mij the new value for the item._x000D_
     *_x000D_
     * @see #get(int, int)_x000D_
     */_x000D_
    @Override_x000D_
    public void update(int i, int j, double mij) {_x000D_
        this.m_totalSum -= get(i, j);_x000D_
        this.m_totalSum += mij;_x000D_
_x000D_
        super.update(i, j, mij);_x000D_
    }_x000D_
_x000D_
    /**_x000D_
     * @see org.jfree.data.xy.MatrixSeries#zeroAll()_x000D_
<SUF>*/_x000D_
    @Override_x000D_
    public void zeroAll() {_x000D_
        this.m_totalSum = 0;_x000D_
        super.zeroAll();_x000D_
    }_x000D_
}_x000D_
