稀疏矩阵实现算法(部分)

类别:Java 点击:0 评论:0 推荐:

MatrixInterface接口中的部分方法还没有实现,你自己来吧,多看看
你们作业中的MatrixInterface接口说明就行了,其实它把实现的思路已经
告诉你了,主要算法它也已经写出了,真的不难,我是实在没有时间


import java.io.*;

/****************************************************************
 This class provides functionality for storing, solving, and
 managing sparce matrices. Only the non-zero entrys are stored.
 It represents the sparse matrix as a two-dimensional linked list
 of the non-zero entrys. Each entry is a Java primitive type int.
****************************************************************/
public class SparseMatrix implements MatrixInterface
{
   // This class provides the list node for the linked list. with
   // references to the next non-zero entry in the same row and
   // the next non-zero entry in the same column.
   class Entry 
   {
      private int row;
      private int column;
      private int value;
      private Entry right;
      private Entry down;
   }

   private Entry[] rowHead;
   private Entry[] columnHead;


   /*************************************************************
    Create an m-by-n SparseMatrix with storing it in the linked 
    list.
    
    @param m the number of rows in this Matrix.
    
    @param n the number of columns in this Matrix.
   **************************************************************/
   public SparseMatrix (int m, int n) //constructor
   {
    if(m <= 0)
    {
    System.out.println("The rows of the matrix must be greater than zero.");
    System.exit(1);
    }
   
    if(n <= 0)
    {
    System.out.println("The columns of the matrix must be greater than zero.");
    System.exit(1);
    }
   
    rowHead = new Entry[m];
    columnHead = new Entry[n];
   }
 

//Return the row dimension of M.
// post: returns the number of rows
 public int numRows()
{
return row;
}  


// Return the column dimension of M.
// post: returns the number of columns
public int numCols()
{
return column;
}


// Set entry M(i,j) to a.
// pre: 1<=i<=numRows(), 1<=j<=numCols()
// post: M(i,j)==a
public void setEntry(int i, int j, int a)
{
//...

}


// Return entry M(i,j).
// pre: 1<=i<=numRows(), 1<=j<=numCols()
// post: returns M(i,j)
public void getEntry(int i, int j, int a)
{
//...

}

//...isZero();copy();transpose();...

/*************************************************************
 Add the passed SparseMatrix to this SparseMatrix.
 
 @param B the SparseMatrix to add to this SparseMatrix.
 
 @return a reference to a new SparseMatrix object that is equal 
 to the sum of this SparseMatrix and the passed one.
 If the passed SparseMatrix is null or the number of rows and 
 columns of this SparseMatrix is not equal to the passed one, null will be returned.
**************************************************************/
   public SparseMatrix add (SparseMatrix B)
   {
    SparseMatrix C = null;
    if (B != null && rowHead.length == B.rowHead.length && columnHead.length == B.columnHead.length)
    {
    C = new SparseMatrix(rowHead.length, columnHead.length);
    SparseList[] scaleOne = new SparseList[rowHead.length * columnHead.length];
   
    Entry aNext, bNext, rowNext, newNode;
   
    for (int i = 0; i < rowHead.length; i++)
    {
    if (rowHead[i] != null || B.rowHead[i] != null)
    {
    aNext = rowHead[i]; //trace Matrix this' row linked list
    bNext = B.rowHead[i]; //trace Matrix B's row linked list
    newNode = new Entry(); //obtain node for new value
    C.rowHead[i] = newNode;
   
    do
    {
    if (aNext == null)
    {
    rowNext = bNext;
    bNext = bNext.right;
    } else if (bNext == null)
    {
    rowNext = aNext;
    aNext = aNext.right;
    } else
    {
    if (bNext.column < aNext.column)
    {
    rowNext = bNext;
    bNext = bNext.right;
    } else if (bNext.column > aNext.column)
    {
    rowNext = aNext;
    aNext = aNext.right;
    } else if (Math.abs(aNext.value + bNext.value) > 0.0001)
    {
    rowNext = new Entry();
    rowNext.row = aNext.row;
    rowNext.column = aNext.column;
    rowNext.value = aNext.value + bNext.value;
    aNext = aNext.right;
    bNext = bNext.right;
    }else // at the same position && sum of the values = 0
    {
    aNext = aNext.right;
    bNext = bNext.right;
    continue;
    }
    } 
       
    newNode.row = rowNext.row;
    newNode.column = rowNext.column;
    newNode.value = rowNext.value;
   
    C.insertColumn(newNode);
   
    rowNext = rowNext.right;
    if (aNext != null || bNext != null)
    {
   newNode.right = new Entry();
   newNode = newNode.right;
} else // the last node
{ newNode.right = null;
newNode.down = null;
}
}while (aNext != null || bNext != null);
}
    }
    }
    return C;
   }


   /*************************************************************
 Multiply the passed number to all entrys in 
 this SparseMatrix.
 
 @param k the number to multiply this SparseMatrix
 by.
 
 @return a reference to a new Matrix object that is equal to
 the product of this SpaseMatrix and the passed
 number.
**************************************************************/
   public SparseMatrix scale (float k)
   {
    SparseMatrix C = new SparseMatrix(rowHead.length, columnHead.length);
    Entry newNode, rowNext;
   
    if (k != 0.) // if k is equal to zero, no new node is added
    {
    for (int i = 0; i < rowHead.length; i++)
    {
    if (rowHead[i] != null)
    {
    rowNext = rowHead[i]; //trace this row linked list
    newNode = new Entry(); //obtain node for new value
    C.rowHead[i] = newNode;
   
    do
    {
    int rowNo = rowNext.row;
    int columnNo = rowNext.column;
   
    newNode.row = rowNo;
    newNode.column = columnNo;
    newNode.value = k * rowNext.value;
   
    C.insertColumn(newNode);
   
    rowNext = rowNext.right;
    if (rowNext != null)
    {
    newNode.right = new Entry();
    newNode = newNode.right;
   } else // the last node
{ newNode.right = null;
newNode.down = null;
}
    }while (rowNext != null);
   }
   }
}
    return C;
   }


/*************************************************************
 Return the maximum absolute row sum of this
 SparseMatrix.
 
 @return the infinity norm of this SparseMatrix.
**************************************************************/
   public float norm ()
   {
    double max = 0.;
    double sum;
    Entry newNode;
   
    for (int i = 0; i < rowHead.length; i++)
    {
    sum = 0.;
    // add the value in the same row list
    for (newNode = rowHead[i]; newNode != null; newNode = newNode.right)
    {
    sum += Math.abs(newNode.value);
    }
    if (sum > max)
    max = sum;
    }
    return (float)max;
   }


/*************************************************************
 Initialize this SparseMatrix with the values 
 from the passed array. 
 
 @param a the array to be initialized this SparseMatrix.
**************************************************************/
   public void setMatrix (SparseList[] a)
   {
      for (int i = 0; i < rowHead.length; i++)
         rowHead[i] = null;

      for (int j = 0; j < columnHead.length; j++)
         columnHead[j] = null;
   
    Entry newNode, rowPrevious, rowNext, columnPrevious, columnNext;
   
    for (int i = 0; i < a.length; i++)
    {
    // test if this Entry is in this matrix and the entry's value is not equal to zero
    if (a[i].getRow() < rowHead.length && a[i].getColumn() < columnHead.length && a[i].getValue() != 0)
    {
    int rowNo = a[i].getRow();
    int columnNo = a[i].getColumn();
   
    newNode = new Entry(); //obtain node for new value
    newNode.right = null;
    newNode.down = null;
    newNode.row = rowNo;
    newNode.column = columnNo;
    newNode.value = a[i].getValue();
   
   // the later one will overwrite the former one if at the same position
   if (rowHead[rowNo] != null && columnNo == rowHead[rowNo].column)
    rowHead[rowNo].value = a[i].getValue();
   else // no former one at the same position
   insertRow(newNode);
    insertColumn(newNode);
    }
    }
   }

/*************************************************************
 Display this SparseMatrix as a rectangular grid
 of values on the screen. It prints all the entrys in the
 matrix, including the implicit zeros.
**************************************************************/
   public void displayMatrix ()
   {
    int count;
    Entry newNode;
    for (int i = 0; i < rowHead.length; i++)
    {
    count = 0; // count the number of the printed entrys
    for (newNode = rowHead[i]; newNode != null; newNode = newNode.right)
    {
    // print the 0's before and between the nodes
    for (int j = count; j < newNode.column; j++)
    System.out.print("0.000    ");
    System.out.print(newNode.value);
    for (int j = 0; j < 9 - new Int(newNode.value).toString().length(); j++)
    System.out.print(" ");
    count = newNode.column + 1;
    }
   
    // print the 0's after the last node or in the empty linked list.
    for (int j = count; j < columnHead.length; j++)
    System.out.print("0.000    ");
System.out.println();
}
System.out.println();
   }
   
          
   // insert to the row linked list
   private void insertRow (Entry node)
   {   
   Entry rowPrevious, rowNext;
int rowNo = node.row;
      int columnNo = node.column;
   
   // see if the node goes first in the list
   if (rowHead[rowNo] == null || columnNo < rowHead[rowNo].column)
   {
    node.right = rowHead[rowNo];
    rowHead[rowNo] = node;
   } else // find place to link the node
   {
    rowPrevious = rowHead[rowNo];
    rowNext = rowHead[rowNo].right;
    while (rowNext != null && columnNo > rowNext.column)
    {
    rowPrevious = rowNext;
    rowNext = rowNext.right;
    }
   
    // adjust links to complete insertion
    rowPrevious.right = node;
    node.right = rowNext;
   }
}
   

// insert to the column linked list
private void insertColumn (Entry node)
{
Entry columnPrevious, columnNext;
int rowNo = node.row;
      int columnNo = node.column;

// see if the node goes first in the list
if (columnHead[columnNo] == null || rowNo < columnHead[columnNo].row)
{
   node.down = columnHead[columnNo];
columnHead[columnNo] = node;
} else // find place to link the node
{
columnPrevious = columnHead[columnNo];
columnNext = columnHead[columnNo].down;
   while (columnNext != null && rowNo > columnNext.row)
   {
    columnPrevious = columnNext;
    columnNext = columnNext.down;
   }
   
   // adjust links to complete insertion
   columnPrevious.down = node;
   node.down = columnNext;
}
}
}

 

 


class SparseList:

// This class holds the (i,j) co-ordinates and values of the non-zero entrys
public class SparseList 
{
   private int row;
   private int column;
   private int value;

   public void setRow (int row) 
   {  this.row = row;
   }

   public void setColumn (int column) 
   { this.column = column;
   }

   public void setValue (int value) 
   {  this.value = value;
   }
   
   public int getRow ()
   { return row;
}

public int getColumn ()
{ return column;
}

public int getValue ()
{ return value;
}
}


//test class
class SparseMatrixTest:

import java.io.*;

/***********************************************
 This is the test for the Matrix class.
***********************************************/
public class SparseMatrixTest
{
   public static void main (String[] args)
   {
      // create SparseMatrix A
System.out.print("Input the command ");
System.out.println("(\"i\" for inputting from keyboard or another key for default):");
      BufferedReader In = new BufferedReader(new InputStreamReader(System.in));
      String inputText;
try
{
inputText = In.readLine();
} catch (IOException IOE)
{
System.out.println(IOE.toString());
return;
}      
      SparseList[] a;
      SparseMatrix A;
      
      if (inputText.equals("i"))
      {
       int row, column, num;
try
{
System.out.print("Input the rows of the SparseMatrix A: ");
row = Integer.parseInt(In.readLine());
System.out.print("Input the columns of the SparseMatrix A: ");
column = Integer.parseInt(In.readLine());
System.out.print("Input the number of entrys in SparseMatrix A: ");
num = Integer.parseInt(In.readLine());
} catch (IOException IOE)
{
System.out.println(IOE.toString());
System.out.println("Unable to get the integer data.");
return;
}
       A = new SparseMatrix(row, column);
       a = new SparseList[num];
       for (int i = 0; i < num; i++)
       {
       System.out.println("The entry " + i + ":");
       a[i] = new SparseList();
       int row_num, column_num;
try
{
System.out.print("Input the row:");
row_num = Integer.parseInt(In.readLine());
System.out.print("Input the column:");
column_num = Integer.parseInt(In.readLine());
} catch (IOException IOE)
{
System.out.println(IOE.toString());
System.out.println("Unable to get the integer data.");
return;

       a[i].setRow(row_num);
       a[i].setColumn(column_num);
       System.out.print("Input the value:");
       int value;
try
{
value = Integer.parseInt(In.readLine());
} catch (IOException IOE)
{
System.out.println(IOE.toString());
System.out.println("Unable to get the double data.");
return;
}
        a[i].setValue(value);
      }
      } else
      {
      a = new SparseList[3];

      a[0] = new SparseList();
      a[0].setRow(4);
      a[0].setColumn(6);
      a[0].setValue(4.0f);

      a[1] = new SparseList();
      a[1].setRow(5);
      a[1].setColumn(3);
      a[1].setValue(6.0f);

      a[2] = new SparseList();
      a[2].setRow(9);
      a[2].setColumn(5);
      a[2].setValue(7.0f);

      A = new SparseMatrix(10,15);
   }

      A.setMatrix(a);
      
      System.out.println("Matrix A:");
      A.displayMatrix();
      
      System.out.println("k * Matrix A:");
      A.scale(5.2f).displayMatrix();
      
      System.out.print("norm |A| = ");
      System.out.println(A.norm() + "\n");
      
      // create SparseMatrix B
System.out.print("Input the command ");
System.out.println("(\"i\" for inputting from keyboard or another key for default):");
try
{
inputText = In.readLine();
} catch (IOException IOE)
{
System.out.println(IOE.toString());
return;

      SparseList[] b;
      SparseMatrix B;
      
      if (inputText.equals("i"))
      {
       int row, column, num;
try
{
System.out.print("Input the rows of the SparseMatrix B: ");
row = Integer.parseInt(In.readLine());
System.out.print("Input the columns of the SparseMatrix B: ");
column = Integer.parseInt(In.readLine());
System.out.print("Input the number of entrys in SparseMatrix B: ");
num = Integer.parseInt(In.readLine());
} catch (IOException IOE)
{
System.out.println(IOE.toString());
System.out.println("Unable to get the integer data.");
return;
}
       B = new SparseMatrix(row, column);
       b = new SparseList[num];
       for (int i = 0; i < num; i++)
       {
       System.out.println("The entry " + i + ":");
       b[i] = new SparseList();
       int row_num, column_num;
try
{
System.out.print("Input the row:");
row_num = Integer.parseInt(In.readLine());
System.out.print("Input the column:");
column_num = Integer.parseInt(In.readLine());
} catch (IOException IOE)
{
System.out.println(IOE.toString());
System.out.println("Unable to get the integer data.");
return;
}
       b[i].setRow(row_num);
b[i].setColumn(column_num);
       System.out.print("Input the value:");
       int value;
try
{
value = Integer.parseInt(In.readLine());
} catch (IOException IOE)
{
System.out.println(IOE.toString());
System.out.println("Unable to get the double data.");
return;
}
        b[i].setValue(value);
      }
      } else
      {
      b = new SparseList[4];

      b[0] = new SparseList();
      b[0].setRow(3);
      b[0].setColumn(7);
      b[0].setValue(14.45f);

      b[1] = new SparseList();
      b[1].setRow(5);
      b[1].setColumn(3);
      b[1].setValue(76.23f);

      b[2] = new SparseList();
      b[2].setRow(3);
      b[2].setColumn(5);
      b[2].setValue(5.0f);
      
      b[3] = new SparseList();
      b[3].setRow(6);
      b[3].setColumn(5);
      b[3].setValue(0.34f);
      
      B = new SparseMatrix(10,15);
}

      B.setMatrix(b);
      
      System.out.println("Matrix B:");
      B.displayMatrix();
      
      System.out.print("norm |B| = ");
      System.out.println(B.norm() + "\n");
      
      System.out.println("Matrix (A + B):");
      if (A.add(B) != null)
       A.add(B).displayMatrix();
      else
       System.out.println("null\n");
   }
}
 

本文地址:http://com.8s8s.com/it/it14569.htm