老康的专栏

老康的个人网站:www.b9527.net(老康之家)

robertb9527 ID:robertb9527
693279次访问,排名50好友0人,关注者0
robertb9527的文章
原创 672 篇
翻译 4 篇
转载 0 篇
评论 1727 篇
老康的公告

---------------

留言本 New!

---------------

个人简介
Name:
老康之家
From:
老康的Nest

---------------

TOP 20

Bruce Eckel-java 评论
各种G,M,Y邀请
eclipse使用第一步
TIOBE程序语言排行榜
韦君新戏
艾莉婕(Alizee)介绍
JDK1.4安装与配置
中医不过是一种...
名牌化妆品官方网站收集
沧海一声笑
五种流行Java IDE比较
第一个Eclipse程序完全图解
稀疏矩阵实现算法(部分)
编写跨平台Java程序需注意
java语音实现技术TTS资料
萧蔷2005年月历
逝去的青春逝去的记忆
给CSDNBlog管理员的信
《功夫》源头活水
恭喜大家发财!!!

---------------
老康的radio Hot!
---------------
Listed on BlogShares
---------------
资源

下载Firefox浏览器

访问sourceForge网站

最近评论
zp4656456:mark
jieyu1987:很好
zhouxz1026:写得真是太好了,赞一个!学习了!
蜂胶
蜂蜜
zhouxz1026:写得真是太好了,赞一个!学习了!
蜂胶
蜂蜜
zhouxz1026:写得真是太好了,赞一个!学习了!
蜂胶
蜂蜜
文章分类
收藏
相册
MyPhoto
linux相关
ChinaUnix
Fedora
LinuxFans
常去论坛
JavaRanch
JavaWorld
常用工具
Eclipse
Hibernate
MySQL
Tomcat
一路上
瀚海星云
友情Blog
|><|神祕花園
【C·K】
LileLTP-Java之路
Loster's Blog
lxgljj的专栏
shaokun305的专栏
sunlen的专栏
YuL's Blog
中国丫头
分享Java快乐
思远空间
易网
老康之家(RSS)
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

原创 稀疏矩阵实现算法(部分)收藏

新一篇: 《Thinking in Patterns with Java》翻译版介绍 | 旧一篇: google搜索之比较篇

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");
   }
}
 

发表于 @ 2004年10月16日 22:51:00|评论(loading...)|编辑

新一篇: 《Thinking in Patterns with Java》翻译版介绍 | 旧一篇: google搜索之比较篇

评论

#mh 发表于2004-11-05 20:32:00  IP: 202.127.200.*
逐字逐句地看完这个帖子以后,我的心久久不能平静,震撼啊!为什么会有如此好的帖子!想我纵横网络数十年,自以为再也不会有任何帖子能打动我,没想到今天看到了如此精妙绝伦的这样一篇帖子。楼主,是你让我深深地理解了‘人外有人,天外有天’这句话。谢谢啊!在看完这帖子后,我没有立即回复,因为我生怕我庸俗不堪的回复会玷污了这少有的帖子。但是我还是回复了,因为觉得如果不能在如此精彩的帖子后面留下自己的名字,那我死也不会瞑目的!楼主,请原谅我的自私!我知道无论用多么华丽的辞藻来形容楼主您帖子的精彩程度都是不够的,都是虚伪的,所以我只想说一句:您的帖子太好看了!我愿意一辈子的看下去!这篇帖子构思新颖,题材独具匠心,段落清晰,情节诡异,跌宕起伏,主线分明,引人入胜,平淡中显示出不凡的文学功底,可谓是字字珠玑,句句经典,是我辈应当学习之典范。就小说艺术的角度而言,这篇帖子不算太成功,但它的实验意义却远远大于成功本身。正所谓:“一马奔腾,射雕引弓,天地都在我心中!”楼主真不愧为无厘界新一代的开山怪!本来我已经对这个社区失望了,觉得这个社区没有前途了,心里充满了悲哀。但是看了你的这个帖子,又让我对社区产生了希望。是你让我的心里重新燃起希望之火,是你让我的心死灰复燃,是你拯救了我一颗拨凉拨凉的心!本来我已经决定不会在社区回任何帖子了,但是看了你的帖子,我告诉自己这个帖子是一定要回的!这是百年难得一见的好贴啊!苍天有眼啊,让我在有生之年得以观得如此精彩绝伦的帖子!
#leng 发表于2004-11-05 20:35:00  IP: 202.127.200.*
我塄
#FlyInAir 发表于2005-03-08 09:32:00  IP: 218.20.62.*
Good.

Good.

I like this article.
发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 老康