This class represents a lower (or upper) triangle matrix that stores ints. : Matrix « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Book
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » MatrixScreenshots 
This class represents a lower (or upper) triangle matrix that stores ints.
 
/**
 * created 23.01.2008
 
 * @by Marc Woerlein ([email protected])
 *
 * Copyright 2008 Marc Woerlein
 
 * This file is part of parsemis.
 *
 * Licence: 
 *  LGPL: http://www.gnu.org/licenses/lgpl.html
 *   EPL: http://www.eclipse.org/org/documents/epl-v10.php
 *   See the LICENSE file in the project's top-level directory for details.
 */

//package de.parsemis.utils;

/**
 * This class represents a lower (or upper) triangle matrix that stores ints.
 
 @author Thorsten Meinl ([email protected])
 @author Marc Woerlein ([email protected])
 
 */
public class HalfIntMatrix{
  /** the array holding the complete triangle matrix */
  private final int[] matrix;

  private final int size, initialValue;

  /**
   * Creates a new HalfIntMatrix that is an exact copy of the given template
   
   @param template
   *            a HalfIntMatrix that should be copied
   */
  public HalfIntMatrix(final HalfIntMatrix template) {
    this(template, 0);
  }

  /**
   * Creates a new HalfIntMatrix that is an exact copy of the given template
   
   @param template
   *            a HalfIntMatrix that should be copied
   @param reserveNewNodes
   *            the number of new nodes for which space should be reserved or
   *            removed
   */
  public HalfIntMatrix(final HalfIntMatrix template, final int reserveNewNodes) {
    this.size = (template.size + reserveNewNodes);
    this.initialValue = template.initialValue;
    this.matrix = new int[((size * size + size2)];
    System.arraycopy(template.matrix, 0, matrix, 0,
        reserveNewNodes >= ? template.matrix.length : matrix.length);
    for (int i = template.matrix.length; i < matrix.length; i++) {
      matrix[i= initialValue;
    }
  }

  /**
   * Creates a new HalfIntMatrix with the given size and initial values
   
   @param initialSize
   *            the size of the matrix in rows (or columns)
   @param initialValue
   *            the initial value of each matrix element
   */
  public HalfIntMatrix(final int initialSize, final int initialValue) {
    this.size = initialSize;
    this.initialValue = initialValue;
    this.matrix = new int[((size * size + size2)];
    for (int i = 0; i < matrix.length; i++) {
      matrix[i= initialValue;
    }
  }

  /*
   * (non-Javadoc)
   
   * @see de.parsemis.utils.IntMatrix#exchangeRows(int, int)
   */
  public void exchangeRows(int rowA, int rowB) {
    if (rowA == rowB) {
      return;
    }
    if (rowA > rowB) {
      final int t = rowA;
      rowA = rowB;
      rowB = t;
    }
    int i = 0;
    for (; i < rowA; i++) {
      swap(rowA, i, rowB, i);
    }
    for (i++; i < rowB; i++) {
      swap(rowA, i, rowB, i);
    }
    for (i++; i < size; i++) {
      swap(rowA, i, rowB, i);
    }
    swap(rowA, rowA, rowB, rowB);
    swap(rowA, rowB, rowB, rowA);
  }

  /*
   * (non-Javadoc)
   
   * @see de.parsemis.utils.IntMatrix#get(int, int)
   */
  public int get(final int row, final int col) {
    assert row >= && col >= && row < size && col < size : "row/col out of bounds: "
        + row + "/" + col + " size: " + size;
    return matrix[idx(row, col)];
  }

  private final int idx(final int row, final int col) {
    if (row < col) {
      return col * (col + 1+ row;
    else {
      return row * (row + 1+ col;
    }

  }

  /*
   * (non-Javadoc)
   
   * @see de.parsemis.utils.IntMatrix#set(int, int, int)
   */
  public void set(final int row, final int col, final int value) {
    assert row >= && col >= && row < size && col < size : "row/col out of bounds: "
        + row + "/" + col + " size: " + size;
    matrix[idx(row, col)] = value;
  }

  /*
   * (non-Javadoc)
   
   * @see de.parsemis.utils.IntMatrix#size()
   */
  public int size() {
    return size;
  }

  private final void swap(final int r1, final int c1, final int r2,
      final int c2) {
    swap(matrix, idx(r1, c1), idx(r2, c2));
  }

  /**
   * swaps the values of the array at position i and j
   
   @param array
   @param i
   @param j
   */
  public static void swap(final int array[]final int i, final int j) {
    final int temp = array[i];
    array[i= array[j];
    array[j= temp;
  }
}

   
  
Related examples in the same category
1.Implementation of a 4x4 matrix suited for use in a 2D and 3D graphics rendering engine
2.Rotations in a three-dimensional spaceRotations in a three-dimensional space
3.The Java Matrix Class provides the fundamental operations of numerical linear algebra
4.Vector extends Matrix
5.A 3x3 matrix implementation
6.4 x 4 Matrix
7.Various geometric transformations on matrix form
8.Inertia Matrix
9.Simulate a matrix. Provides method to travers vectors that compose the matrix.
ww_w__.__j___a_v_a2__s__.c__o_m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.