矩阵运算(一)

Python语言实现

因为python有专门的软件包,所以用Python实现非常简单。

#导入科学计算包

import numpy as np;

#随机生成一个3 * 4的整型矩阵,元素的最大值为10

print(data)

#对矩阵进行转置运算

data2 = data.T

print(data2)

#对矩阵进行乘法运算

data3 = data * data2

print(data3)

C语言实现

// Created by 李印臣 on 2018/11/26.

// Copyright © 2018年 李印臣. All rights reserved.

//

#include

#include

#include

#define true1

#define false

typedefintbool;

// 定义矩阵结构体。矩阵中的元素用分数表示,方便矩阵运算(尤其是除法和求逆)是比较大小,保留精度。分数由分子、分母两个整数组成。isInt标识用于简化矩阵元素全为整数时的运算。

structMatrix{

boolisInt;

int***arr;

};

//初始化矩阵。这里要注意指针的层级关系,并对isInt初始化。y还有一点需要强调:memset的单位是char,如memset(matrix->arr[i][j], 1, n * sizeof(int));,则每一个int值将被设置成00000001000000010000000100000001(16843009)

structMatrix* initMatrix(intm,intn){

inti =, j =;

structMatrix*matrix =NULL;

matrix = (structMatrix*)malloc(sizeof(structMatrix));

matrix->arr= (int***)malloc(sizeof(int**) *2);

for(i =; i

matrix->arr[i] = (int**)malloc(sizeof(int*) * m);

for(j =; j

matrix->arr[i][j] = (int*)malloc(sizeof(int*) * n);

memset(matrix->arr[i][j],, n *sizeof(int));

}

}

for(i =; i

for(j =; j

matrix->arr[1][i][j] =1;

}

}

matrix->isInt=true;

returnmatrix;

}

voidfreeMatrix(structMatrix*matrix,intm){

inti =, j =;

for(i =; i

for(j =; j

free(matrix->arr[i][j]);

}

free(matrix->arr[i]);

}

free(matrix->arr);

free(matrix);

}

//矩阵转置

structMatrix* transpose(structMatrix*matrix,intm,intn,boolisInt){

inti =, j =;

structMatrix*newMatrix =NULL;

newMatrix =initMatrix(n, m);

if(isInt){

for(i =; i

for(j =; j

newMatrix->arr[][j][i] = matrix->arr[][i][j];

}

}

}else{

for(i =; i

for(j =; j

newMatrix->arr[][j][i] = matrix->arr[][i][j];

newMatrix->arr[1][j][i] = matrix->arr[1][i][j];

}

}

}

newMatrix->isInt= matrix->isInt;

returnnewMatrix;

}

//矩阵乘法只有在第一个矩阵的列数(column)和第二个矩阵的行数(row)相同时才有意义。

//结果矩阵的行数等于矩阵matrix的行数,列数等于matrix2的列数。

//结果矩阵的第m行第n列的元素等于矩阵matrix的第m行的元素与矩阵matrix2的第n列对应元素乘积之和。

structMatrix*multiply(structMatrix*matrix,structMatrix*matrix2,intm,intn,intp){

inti =, j =, k =;

inta =, b =;

structMatrix*newMatrix =NULL;

newMatrix =initMatrix(m, p);

if(matrix->isInt&& matrix2->isInt){

for(i =; i

for(j =; j

for(k =; k

newMatrix->arr[][i][j] += matrix->arr[][i][k] * matrix2->arr[][k][j];

}

}

}

}

//如果矩阵元素存在分数,则先对两个分数做乘法,再对两个分数做加法(需要统分)

elseif(!matrix->isInt&& !matrix2->isInt){

for(i =; i

for(j =; j

for(k =; k

a = matrix->arr[][i][k] * matrix2->arr[][k][j];

b = matrix->arr[1][i][k] * matrix2->arr[1][k][j];

newMatrix->arr[][i][j] += newMatrix->arr[][i][j] * b + a * newMatrix->arr[1][i][j];

newMatrix->arr[1][i][j] *= b;

}

}

}

}elseif(!matrix->isInt&& matrix2->isInt){

for(i =; i

for(j =; j

for(k =; k

a = matrix->arr[][i][k] * matrix2->arr[][k][j];

b = matrix->arr[1][i][k];

newMatrix->arr[][i][j] += newMatrix->arr[][i][j] * b + a * newMatrix->arr[1][i][j];

newMatrix->arr[1][i][j] *= b;

}

}

}

}else{

for(i =; i

for(j =; j

for(k =; k

a = matrix->arr[][i][k] * matrix2->arr[][k][j];

b = matrix2->arr[1][k][j];

newMatrix->arr[][i][j] += newMatrix->arr[][i][j] * b + a * newMatrix->arr[1][i][j];

newMatrix->arr[1][i][j] *= b;

}

}

}

}

newMatrix->isInt= matrix->isInt& matrix2->isInt;

returnnewMatrix;

}

intmain(intargc,constchar* argv[]) {

intm =, n =, i =, j =;

structMatrix*matrix;

scanf("%d %d", &m, &n);

matrix =initMatrix(m, n);

matrix->arr[][2][3] =6;

matrix->arr[1][2][3] =5;

matrix->isInt=false;

if(matrix->isInt){

for(i =; i

for(j =; j

printf("%-10d ", matrix->arr[][i][j]);

}

printf("\n");

}

}else{

for(i =; i

for(j =; j

if(matrix->arr[1][i][j] ==1|| matrix->arr[][i][j] % matrix->arr[1][i][j] ==){

printf("%-10d ", matrix->arr[][i][j]);

}else{

printf("%-7.2f ", ((float)matrix->arr[][i][j]) / matrix->arr[1][i][j]);

}

}

printf("\n");

}

}

matrix =multiply(matrix,transpose(matrix, m, n, matrix->isInt), m, n, m);

if(matrix->isInt){

for(i =; i

for(j =; j

printf("%-10d ", matrix->arr[][i][j]);

}

printf("\n");

}

}else{

for(i =; i

for(j =; j

if(matrix->arr[1][i][j] ==1|| matrix->arr[][i][j] % matrix->arr[1][i][j] ==){

printf("%-10d ", matrix->arr[][i][j]);

}else{

printf("%-7.2f ", ((float)matrix->arr[][i][j]) / matrix->arr[1][i][j]);

} }

printf("\n");

}

}

free(matrix);

return;

}

C语言实现矩阵运算比较繁琐,本例依然存在很多不完善的地方。比如,测试数据的输入,没有对两个矩阵的行数和列数检测,也没有化简分数,输出打印的格式不能满足所有范围内的数值。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181127G07LC800?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

同媒体快讯

扫码关注云+社区

领取腾讯云代金券