标准c重写QR分解算法,用来计算线性或超定方程组,计算结果和matlab一致

对上版一版本的代码做了内存优化,修正了非必要的内存分配,计算结果与matlab的qr命令运算结果一致。请自行按main中提供的自己自行修改数据,进行验证。

#include <math.h>
#include <stdlib.h>
#include <memory.h>
#include <iostream>

using namespace std;

double dot(double * a, double * b, int len){
    double result = 0;
    for (int i = 0; i<len; i++)
        result += a[i] * b[i];
    return result;
}

//列向量与行向量乘法
void mulvector(double * a, double * b, int len, double cof, double * c){
    for (int i = 0; i < len; ++i)
        for (int j = 0; j < len; ++j)
            c[i * len + j] = cof * a[i] * b[j];
    return;
}


//矩阵与矩阵,矩阵与向量相乘
bool mulmatrix(double * a, int ar, int ac, double * b, int br, int bc, double  * c)
{
    //c为输出矩阵
    if (ac != br)
        return false;
    else
    {
        for (int i = 0; i < ar; ++i)
        {
            for (int j = 0; j < bc; ++j)
            {
                double sum = 0;
                for (int k = 0; k < ac; ++k)
                    sum += a[i * ac + k] * b[k * bc + j];

                c[i*bc + j] = sum;
            }
        }
        return true;
    }
}

void transpose(double * coef, int

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值