求教关于矩阵的问题(50分)

zhouyong80 2006-06-21 11:28:59
用java语言实现一个类Matrix,用来表示矩阵,所实现的类应该支持下面的操作:
●根据给定的行和列的数目创建一个矩阵。
●在某个给定的行和列的位置上取得或设置元素c
●对两个相容的矩阵进行相加和相乘运算:
并提供一个相应的测试程序。
这是我在一本书上看到的题,完全没头绪,请达人帮忙做一下.谢谢
...全文
293 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
imA 2006-06-21
  • 打赏
  • 举报
回复
这个有什么难的吗?

矩阵就是一个多维数组,也就是说对矩阵的处理就是java中对多维数组的处理。

关于矩阵的运算法则,数学上不是有公式嘛,把数学公式用java语言实现一下就可以了。
For_suzhen 2006-06-21
  • 打赏
  • 举报
回复
数学家的问题
kevinliuu 2006-06-21
  • 打赏
  • 举报
回复
up
eqxu 2006-06-21
  • 打赏
  • 举报
回复

用native

调用matlab

这个是最经典的了的

根本没法再经典了
f_acme 2006-06-21
  • 打赏
  • 举报
回复
发现还是写错了一些了,那个col和row不能直接取得,呵呵
f_acme 2006-06-21
  • 打赏
  • 举报
回复
public class Marix {

/**
* @param args
*/
private int[][] mar;
private int row;
private int col;
public Marix(int i,int j){
this.mar = new int[i][j];
this.row = i;
this.col = j;
}

public int getElement(int i,int j) throws Exception{
if(i>this.row || j>this.col)
throw new Exception("index out of bound.");
return mar[i][j];
}

public void setElement(int i,int j,int val) throws Exception{
if(i>this.row || j>this.col)
throw new Exception("index out of bound.");
mar[i][j]=val;
}

public Marix add(Marix another) throws Exception
{
if(this.row != another.row || this.col != another.col)
throw new Exception("the row or col is not equal.");
Marix ret = new Marix(this.row, this.col);
for(int i=0; i<this.row; i++)
{
for(int j=0; j<this.col; j++)
ret.setElement(i, j, this.getElement(i, j) + another.getElement(i, j));
}
return ret;
}

public Marix multiply(Marix another) throws Exception
{
if(this.row != another.col)
throw new Exception("the row is not equal to col.");
Marix ret = new Marix(this.row, this.col);
int temp =0;
for(int i=0; i<this.row; i++)
{
temp = 0;
for(int j=0; j<this.row; j++)
{
for(int k=0; k<another.col; k++)
temp += this.getElement(i, k) * another.getElement(k, j);
ret.setElement(i, j, temp);
}
}
return ret;
}

public void display()
{
for(int i=0; i<this.row; i++)
{
for(int j=0; j<this.col; j++)
System.out.printf(mar[i][j]+ " ");
System.out.println("");
}
}

public static void main(String[] args) throws Exception{
Marix m1=new Marix(4,4);
m1.setElement(1,2,3);
m1.display();
System.out.println("");

Marix m2 = new Marix(4,4);
m2.setElement(2,3,4);
m2.display();
System.out.println("");

m1= m1.add(m2);
m1.display();
System.out.println("");

m1= m1.multiply(m2);
m1.display();
System.out.println("");
}
}
f_acme 2006-06-21
  • 打赏
  • 举报
回复
分乃身外之物,^_^
Jeremyzhou 2006-06-21
  • 打赏
  • 举报
回复
package com.jeremy;

public class Marix {

/**
* @param args
*/
private int[][] mar;
public Marix(int i,int j){
mar=new int[i][j];
}
public int getElement(int i,int j){
int value=0;
value=mar[i][j];
return value;
}
public void setElement(int i,int j,int val){
mar[i][j]=val;
}
public static void main(String[] args){
Marix m=new Marix(3,4);
m.setElement(1,2,3);
System.out.println(m.getElement(1,2));
}
其他方法.....
}


给我分 一分也不能少..我穷死了..

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧