Matrix(1133)
来源:互联网 发布:linux查看硬盘分区 编辑:程序博客网 时间:2024/05/16 08:37
今天看了算法如下:1.求一个二维数组i和j互质的情况,就是求i和j的公约数只能为1.采用求最大公约数的算法实现。
2.求向量的相乘,具体实现算法:需要判断两个数组长度是否一致。
3.求两个二维数组的乘积,主要是注意行等于列的情况,中间值k一定为第一个数组的列或者是第二个数组的行。
1.向量乘积:
result += a[i]*b[i];
具体代码:
public static double dot(double[] x, double[] y){//向量点乘
double result = 0;
if(x.length == y.length){
for (int i = 0;i < x.length;i ++)
result += x[i] * y[i];//不用加括号,优先级是不一样的
}else{
StdOut.println("Error!");
return result;
}
return result;
}
2.向量相乘:MxN * NxP, M与P不一定相等,因为超出的部分为零。
其中设置共同的部分N为k值。
代码如下:
public static double[][] mult(double[][] a, double[][] b){//矩阵与矩阵之积 [M][N] * [N][P] = [M][P]
if (a[0].length == b.length){
double[][] result =new double[a.length][b[0].length];//第一个是行,第二个是列
for(inti = 0; i < result.length;i ++)
for (int j = 0;j < result[i].length;j ++)
for (int k = 0;k < b.length;k ++) //这里b.length 可以换为 a[0].length
result[i][j] += a[i][k] * b[k][j];
return result;
}else{
StdOut.println("Error!!!");
return null;
}
}
3.转置矩阵:
就是从新设置一个数组,让他的大小满足转置后的条件,比如[i][j] 变为[j][i] .将原来数组的内容复制过去就可以了。
代码如下:
public static double[][] transpose(double[][] a){ //转置矩阵
double[][] result = new double[a[0].length][a.length];
for(int i =0; i < a.length; i ++){
for (int j = 0;j < a[0].length;j ++){
result[i][j] = a[j][i];
}
}
return result;
}
阅读全文
0 0
- Matrix(1133)
- Matrix结构体(Leap::Matrix)
- 邻接矩阵(Adjacency Matrix)
- 邻接矩阵(Adjacency Matrix)
- Matrix(THUOI2011)
- (二维) Matrix (P2155)
- Zigzag Matrix(模拟)
- Matrix(矩阵操作)
- Matrix(方法实现)
- Matrix multiplication(bitset)
- HDU5569 matrix(dp)
- Spiral Matrix(medium)
- Matrix Power Series(?)
- 矩阵(Matrix)
- Matrix Again (hdu3376)
- matrix (dp)
- The Matrix(黑客帝国)
- 《Matrix》
- P1095 守望者的逃离【2007普及】(贪心)
- Java https请求
- 关于Android 7.0系统通知声音不能播放
- 包装对象的理解
- tableIndex
- Matrix(1133)
- poi导出格式设置
- Failed to install Tomcat6 service,终止,忽略,重试等
- java实现二叉树的先序遍历,中序遍历,后序遍历
- delphi dll 使用
- i.MX6Ul arm linux 开机自动root登陆系统及自动运行程序
- easyui-databox日期验证
- qt中使用dll库的方法
- react flux的坑之(四) 真机调试 Packager can't listen on port 8081