1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ---------------------------
  25. * NormalizedMatrixSeries.java
  26. * ---------------------------
  27. * (C) Copyright 2003-2005, by Barak Naveh and Contributors.
  28. *
  29. * Original Author: Barak Naveh;;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: NormalizedMatrixSeries.java,v 1.2 2005/01/11 16:42:26 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
  37. *
  38. */
  39. package org.jfree.data.xy;
  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. public class NormalizedMatrixSeries extends MatrixSeries {
  49. /** The default scale factor. */
  50. public static final double DEFAULT_SCALE_FACTOR = 1.0;
  51. /**
  52. * A factor that multiplies each item in this series when observed using getItem method.
  53. */
  54. private double m_scaleFactor = DEFAULT_SCALE_FACTOR;
  55. /** The sum of all items in this matrix */
  56. private double m_totalSum;
  57. /**
  58. * Constructor for NormalizedMatrixSeries.
  59. *
  60. * @param name the series name.
  61. * @param rows the number of rows.
  62. * @param columns the number of columns.
  63. */
  64. public NormalizedMatrixSeries(String name, int rows, int columns) {
  65. super(name, rows, columns);
  66. /*
  67. * we assum super is always initialized to all-zero matrix, so the
  68. * total sum should be 0 upon initialization. However, we set it to
  69. * Double.MIN_VALUE to get the same effect and yet avoid division by 0
  70. * upon initialization.
  71. */
  72. this.m_totalSum = Double.MIN_VALUE;
  73. }
  74. /**
  75. * Returns an item.
  76. *
  77. * @param itemIndex the index.
  78. *
  79. * @return The value.
  80. *
  81. * @see org.jfree.data.xy.MatrixSeries#getItem(int)
  82. */
  83. public Number getItem(int itemIndex) {
  84. int i = getItemRow(itemIndex);
  85. int j = getItemColumn(itemIndex);
  86. double mij = get(i, j) * this.m_scaleFactor;
  87. Number n = new Double(mij / this.m_totalSum);
  88. return n;
  89. }
  90. /**
  91. * Sets the factor that multiplies each item in this series when observed
  92. * using getItem mehtod.
  93. *
  94. * @param factor new factor to set.
  95. *
  96. * @see #DEFAULT_SCALE_FACTOR
  97. */
  98. public void setScaleFactor(double factor) {
  99. this.m_scaleFactor = factor;
  100. }
  101. /**
  102. * Returns the factor that multiplies each item in this series when
  103. * observed using getItem mehtod.
  104. *
  105. * @return The factor
  106. */
  107. public double getScaleFactor() {
  108. return this.m_scaleFactor;
  109. }
  110. /**
  111. * @see org.jfree.data.xy.MatrixSeries#update(int, int, double)
  112. */
  113. public void update(int i, int j, double mij) {
  114. this.m_totalSum -= get(i, j);
  115. this.m_totalSum += mij;
  116. super.update(i, j, mij);
  117. }
  118. /**
  119. * @see org.jfree.data.xy.MatrixSeries#zeroAll()
  120. */
  121. public void zeroAll() {
  122. this.m_totalSum = 0;
  123. super.zeroAll();
  124. }
  125. }