avx指令+openmp多线程实现一个基本算法作业 c++

本文介绍如何使用AVX指令和OpenMP多线程技术,优化一个C++基础算法作业,通过数学分析和point类的设计,提升程序运行效率。
摘要由CSDN通过智能技术生成

原代码
数学思路
在这里插入图片描述

point类

//文件1,类的定义,point.h
#ifndef _POINT_H
#define _POINT_H

class Point{
   
public:
    Point(float x=0, float y=0) :x(x), y(y) {
   }
    float GetX() const {
   return x;}
    float GetY() const {
   return y;}
private:
    float x,y;
};
#endif

主函数

//主函数,main.cpp
#include "point.h"
#include <iostream>
#include <cmath>
using namespace std;

//直线线性拟合,points 为各点,nPoint 为点数
float lineFit(const Point points[], int nPoint) //友元函数体
{
   
    float avgX=0,avgY=0;    //定义变量
    float lxx=0,lyy=0,lxy=0;
    for(int i=0;i<nPoint;i++)   //计算X、Y的平均值
    {
   
        avgX+=points[i].GetX()/nPoint;
        avgY+=points[i].GetY()/nPoint;
    }
    for(int i=0;i<nPoint;i++)   //计算Lxx、Lyy和Lxy
    {
   
        lxx+=(points[i].GetX()-avgX)*(points[i].GetX()-avgX);
        lyy+=(points[i].GetY()-avgY)*(points[i].GetY()-avgX);
        lxy+=(points[i].GetX()-avgX)*(points[i].GetY()-avgY);
    }
    cout<<"This line can be fitted by y=ax+b."<<endl;
    cout<<"a="<<lxy/lxx;  //输出回归系数a
    cout<<" b="<<avgY-lxy*avgX/lxx<<endl;   //输出回归系数b
    return float(lxy/sqrt(lxx*lyy)); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值