matlab2c使用c++实现matlab函数系列教程-inv函数
来源:互联网 发布:c语言多线程 编辑:程序博客网 时间:2024/06/03 14:58
全栈工程师开发手册 (作者:栾鹏)
matlab2c动态链接库下载
matlab库函数大全
matlab2c基础教程
matlab2c开发全解教程
matlab2c调用方法:
1、下载动态链接库
2、将Matlab2c.dll拷贝到exe同目录下
3、将Matlab2c.h、Matlab2c.lib放到项目头文件目录下
4、在cpp文件中引入下面的代码
#include "Matlab2c.h"#pragma comment(lib,"Matlab2c.lib") using namespace Matlab2c;
matlab中inv函数简介
1、inv函数:求矩阵的逆
2、用法说明
A=inv(B),其中B是输入的可逆矩阵,输出A就是B的逆矩阵,逆矩阵满足性质 AB=BA=E (E是单位阵)。如果输入的是不可逆矩阵会弹出警告,并返回inf
inv的c++源码实现
求矩阵的逆
输入:为可逆方矩阵 输出:矩阵的逆。不可逆时返回null
void swap(double *a,double *b); //声明子程序Matrix Matlab2c::inv(Matrix& a){ if (a.row!=a.column) { throw_logic_error("求逆矩阵不为方阵"); return NULL; } int *is,*js,i,j,k; int n=a.row; double temp,fmax; Matrix p(a.row,a.column); for(i=0;i<n;i++) { for(j=0;j<n;j++) p(i,j)=a(i,j); } is=new int[n]; js=new int[n]; for(k=0;k<n;k++) { fmax=0.0; for(i=k;i<n;i++) for(j=k;j<n;j++) { temp=std::fabs(p(i,j));//找最大值 if(temp>fmax) { fmax=temp; is[k]=i;js[k]=j; } } if((fmax+1.0)==1.0) { delete[] is; delete[] js; throw_logic_error("不存在逆矩阵"); return NULL; } if((i=is[k])!=k) for(j=0;j<n;j++) swap(p.data+k*n+j,p.data+i*n+j);//交换指针 if((j=js[k])!=k) for(i=0;i<n;i++) swap(p.data+i*n+k,p.data+i*n+j); //交换指针 p(k,k)=1.0/p(k,k); for(j=0;j<n;j++) if(j!=k) p(k,j)*=p(k,k); for(i=0;i<n;i++) if(i!=k) for(j=0;j<n;j++) if(j!=k) p(i,j)=p(i,j)-p(i,k)*p(k,j); for(i=0;i<n;i++) if(i!=k) p(i,k)*=-p(k,k); } for(k=n-1;k>=0;k--) { if((j=js[k])!=k) for(i=0;i<n;i++) swap((p.data+j*n+i),(p.data+k*n+i)); if((i=is[k])!=k) for(j=0;j<n;j++) swap((p.data+j*n+i),(p.data+j*n+k)); } delete[] is; delete[] js; return p;}void swap(double *a,double *b){ double c; c=*a; *a=*b; *b=c;}
inv函数的使用测试
#include "Matlab2c.h"#pragma comment(lib,"Matlab2c.lib") using namespace Matlab2c;int main(){ double a[]={1,2,3,2,2,1,3,4,3}; Matrix aa(3,3,a); Matrix bb = Matlab2c::inv(aa); cout<<bb.toString()<<endl; system("pause"); return 0;}
阅读全文
1 0
- matlab2c使用c++实现matlab函数系列教程-inv函数
- matlab2c使用c++实现matlab函数系列教程-blkdiag函数
- matlab2c使用c++实现matlab函数系列教程-compan函数
- matlab2c使用c++实现matlab函数系列教程-eye函数
- matlab2c使用c++实现matlab函数系列教程-hankel函数
- matlab2c使用c++实现matlab函数系列教程-hilb函数
- matlab2c使用c++实现matlab函数系列教程-ones函数
- matlab2c使用c++实现matlab函数系列教程-zeros函数
- matlab2c使用c++实现matlab函数系列教程-pascal函数
- matlab2c使用c++实现matlab函数系列教程-toeplitz函数
- matlab2c使用c++实现matlab函数系列教程-wilkinson函数
- matlab2c使用c++实现matlab函数系列教程-diag函数
- matlab2c使用c++实现matlab函数系列教程-tril函数
- matlab2c使用c++实现matlab函数系列教程-triu函数
- matlab2c使用c++实现matlab函数系列教程-fliplr函数
- matlab2c使用c++实现matlab函数系列教程-flipud函数
- matlab2c使用c++实现matlab函数系列教程-reshape函数
- matlab2c使用c++实现matlab函数系列教程-rot90函数
- [笔记分享] [Android] Android的Linux内核与驱动
- SpringBoot事务注解@Transactional
- 字符集详解(一看就懂系列)
- 《机器学习》读书笔记 6 第4章 决策树
- Hibernate Part6
- matlab2c使用c++实现matlab函数系列教程-inv函数
- hdu 4489 The King’s Ups and Downs【递推】
- python(基础)
- Linux中断体系结构
- 解决DESCryptoServiceProvider加解密时弱密钥异常
- 将IIS请求转发到Tomcat
- caffe中的注意点和理解点
- 判断当前时间是否在某个时间段内
- jsp:jstl标签使用