没有合适的资源?快使用搜索试试~ 我知道了~
C语言矩形运算.......
需积分: 12 0 下载量 89 浏览量
2022-08-04
17:34:46
上传
评论
收藏 2.99MB PDF 举报
温馨提示
试读
24页
C语言矩形运算
资源详情
资源评论
资源推荐
前言
这里总结下C++代码转C代码时,对于矩阵运算的C语言封装。在此申明,此处参考了网上的一些经验
帖,以及增加了自己的一些封装与完善,仅供大家学术参考。如有不当之处,还请批评指正。
C语言实现的矩阵运算汇总
矩阵的定义
头文件:matrix_operation.h
typedef struct {
int rows;
int cols;
double** data;
}Matrix;
#include <stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "math.h"
#include "assert.h"
#include <stdarg.h>
//为矩阵分配初始空间
Matrix matrixAlloc(int rows, int cols);
//释放空间
void matrixFree(Matrix m);
//初始化矩阵
void matrixSet(Matrix m, ...);
//转换为单位矩阵
void matrixSetIdentity(Matrix m);
//复制矩阵
Matrix matrixCopy(Matrix source);
//输出矩阵
void matrixPrint(Matrix m, char *str);
//矩阵相加
Matrix matrixAdd(Matrix a, Matrix b);
//矩阵a+b+c
Matrix matrixAddThree(Matrix a, Matrix b, Matrix c);
//矩阵相减
Matrix matrixSubstract(Matrix a, Matrix b);
//用单元矩阵减去该矩阵
void matrixSubstractFromIdentity(Matrix a);
//矩阵相乘
Matrix matrixMultiply(Matrix input_a, Matrix input_b);
//矩阵a*b*c
Matrix matrixMultiplyThree(Matrix input_a, Matrix input_b, Matrix input_c);
//乘以一个矩阵的转置矩阵.
Matrix matrixMultiplyByTranspose(Matrix a, Matrix b);
//矩阵转置
Matrix matrixTranspose(Matrix input);
//两矩阵是否相等
int matrixEqual(Matrix a, Matrix b, double tolerance);
//矩阵乘以一个系数
void matrixMultiScale(Matrix m, double scalar);
//交换矩阵的两行
void matrixSwapRow(Matrix m, int r1, int r2);
//矩阵某行乘以一个系数
void matrixRowMultiConstant(Matrix m, int r, double scalar);
//Add scalar * row r2 to row r1.
void matrix_shear_row(Matrix m, int r1, int r2, double scalar);
//矩阵的求逆(借鉴他人)
/* Uses Gauss-Jordan elimination.
The elimination procedure works by applying elementary row
operations to our input matrix until the input matrix is reduced to
the identity matrix.
Simultaneously, we apply the same elementary row operations to a
separate identity matrix to produce the inverse matrix.
If this makes no sense, read wikipedia on Gauss-Jordan elimination.
This is not the fastest way to invert matrices, so this is quite
possibly the bottleneck. */
int matrixInverse(Matrix input, Matrix output);
//similar to Eigen::Block
Matrix matrixBlock(Matrix input, int begin_row, int begin_col, int row, int
col);
//欧拉角转旋转矩阵
Matrix matrix_eulerAnglesToRotation(Matrix input);
//旋转矩阵转欧拉角
Matrix matrix_Rotation2eulerAngles(Matrix input);
//将input矩阵模块化赋值给大矩阵output
void matrixAssignValueFromBlock(Matrix input, int begin_row, int begin_col, int
row, int col, Matrix output);
//矩阵求二范数
double matrixNorm(Matrix input);
函数实现:matrix_operation.c
//矩阵标准化
void matrixNormalized(Matrix input);
//矩阵赋值为0
void matrixSetZero(Matrix input);
//矩阵点乘
double matrixDot(Matrix a, Matrix b);
//矩阵叉乘
Matrix matrixCross(Matrix a, Matrix b);
//矩阵求秩
double determinant3d(Matrix M);
//矩阵取出某一列
Matrix matrixCol(Matrix src, int col);
//矩阵正交化
Matrix ortho_normalize(Matrix input_r);
Matrix matrixAlloc(int rows, int cols)
{
Matrix m;
int i;
int j;
m.rows = rows;
m.cols = cols;
m.data = (double**)malloc(sizeof(double*) * m.rows);
for(i = 0; i < m.rows; ++i)
{
m.data[i] = (double*)malloc(sizeof(double) * m.cols);
assert(m.data[i]);
for(j = 0; j < m.cols; ++j)
{
m.data[i][j] = 0.0;
}
}
return m;
}
//释放空间
void matrixFree(Matrix m)
{
int i;
assert(m.data != NULL);
for(i = 0; i < m.rows; ++i)
{
free(m.data[i]);
}
free(m.data);
}
void matrixSet(Matrix m, ...)
{
va_list ap;
int i, j;
va_start(ap, m);
for(i = 0; i < m.rows; i++)
{
for(j = 0; j < m.cols; j++)
{
m.data[i][j] = va_arg(ap,double);
}
}
va_end(ap);
}
void matrixSetIdentity(Matrix m) {
int i;
int j;
assert(m.rows == m.cols);
for ( i = 0; i < m.rows; ++i) {
for ( j = 0; j < m.cols; ++j) {
if (i == j) {
m.data[i][j] = 1.0;
} else {
m.data[i][j] = 0.0;
}
}
}
}
/* 复制矩阵 */
Matrix matrixCopy(Matrix source)
{
Matrix destination = matrixAlloc(source.rows, source.cols);
int i;
int j;
assert(source.rows == destination.rows);
assert(source.cols == destination.cols);
for ( i = 0; i < source.rows; ++i) {
for ( j = 0; j < source.cols; ++j) {
destination.data[i][j] = source.data[i][j];
}
}
return destination;
}
/* 打印矩阵 */
void matrixPrint(Matrix m, char *str) {
printf("%s\n", str);
for ( int i = 0; i < m.rows; ++i) {
for ( int j = 0; j < m.cols; ++j) {
if (j > 0) {
printf(" ");
}
printf("%lf", m.data[i][j]);
剩余23页未读,继续阅读
shuhuangyan
- 粉丝: 2
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0