求c++复数矩阵奇异值分解代码(svd)

求c++复数矩阵奇异值分解代码(svd),第1张

/

本程序在linux g++下编译通过

bool svd(vector<vector<double> > A, int K, vector<vector<double> > &U, vector<double> &S, vector<vector<double> > &V);

A: 输入待分解矩阵

K: 输入,取前K大奇异值及奇异向量

U[0],U[1],,U[K-1]: 前K大奇异值对应的左奇异向量

S[0],S[1],,S[K-1]: 前K大奇异值 S[0]>=S[1]>=>=S[K-1]

V[0],V[1],,V[K-1]: 前K大奇异值对应的右奇异向量

/

#include <cmath>

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <cstring>

#include <fstream>

#include <vector>

using namespace std;

const int MAX_ITER=100000;

const double eps=00000001;

double get_norm(double x, int n){

    double r=0;

    for(int i=0;i<n;i++)

        r+=x[i]x[i];

    return sqrt(r);

}

double normalize(double x, int n){

    double r=get_norm(x,n);

    if(r<eps)

        return 0;

    for(int i=0;i<n;i++)

        x[i]/=r;

    return r;

}

inline double product(doublea, double b,int n){

    double r=0;

    for(int i=0;i<n;i++)

        r+=a[i]b[i];

    return r;

}

void orth(double a, double b, int n){//|a|=1

    double r=product(a,b,n);

    for(int i=0;i<n;i++)

        b[i]-=ra[i];

    

}

bool svd(vector<vector<double> > A, int K, vector<vector<double> > &U, vector<double> &S, vector<vector<double> > &V){

    int M=Asize();

    int N=A[0]size();

    Uclear();

    Vclear();

    Sclear();

    Sresize(K,0);

    Uresize(K);

    for(int i=0;i<K;i++)

        U[i]resize(M,0);

    Vresize(K);

    for(int i=0;i<K;i++)

        V[i]resize(N,0);

    

    srand(time(0));

    double left_vector=new double[M];

    double next_left_vector=new double[M];

    double right_vector=new double[N];

    double next_right_vector=new double[N];

    int col=0;

    for(int col=0;col<K;col++){

        double diff=1;

        double r=-1;

        while(1){

            for(int i=0;i<M;i++)

                left_vector[i]= (float)rand() / RAND_MAX;

            if(normalize(left_vector, M)>eps)

                break;

        }

        for(int iter=0;diff>=eps && iter<MAX_ITER;iter++){

            memset(next_left_vector,0,sizeof(double)M);

            memset(next_right_vector,0,sizeof(double)N);

            for(int i=0;i<M;i++)

                for(int j=0;j<N;j++)

                    next_right_vector[j]+=left_vector[i]A[i][j];

            r=normalize(next_right_vector,N);

            if(r<eps) break;

            for(int i=0;i<col;i++)

                orth(&V[i][0],next_right_vector,N);

            normalize(next_right_vector,N);

            for(int i=0;i<M;i++)

                for(int j=0;j<N;j++)

                    next_left_vector[i]+=next_right_vector[j]A[i][j];

            r=normalize(next_left_vector,M);

            if(r<eps) break;

            for(int i=0;i<col;i++)

                orth(&U[i][0],next_left_vector,M);

            normalize(next_left_vector,M);

            diff=0;

            for(int i=0;i<M;i++){

                double d=next_left_vector[i]-left_vector[i];

                diff+=dd;

            }

            memcpy(left_vector,next_left_vector,sizeof(double)M);

            memcpy(right_vector,next_right_vector,sizeof(double)N);

        }

        if(r>=eps){

            S[col]=r;

            memcpy((char )&U[col][0],left_vector,sizeof(double)M);

            memcpy((char )&V[col][0],right_vector,sizeof(double)N);

        }else{

            cout<<r<<endl;

            break;

        }

    }

    delete [] next_left_vector;

    delete [] next_right_vector;

    delete [] left_vector;

    delete [] right_vector;

    return true;

}

void print(vector<vector<double> > &A){

}

int main(){

    int m=10;

    int n=8;

    int k=5;

    //分解一个108的矩阵A,求其前5个奇异值和奇异向量

    srand(time(0));

    vector<vector<double> > A;

    Aresize(m);

    

    for(int i=0;i<m;i++){

        A[i]resize(n);

        for(int j=0;j<n;j++)

            A[i][j]=(float)rand()/RAND_MAX-05;

    }

    

    cout<<"A="<<endl;

    for(int i=0;i<Asize();i++){

        for(int j=0;j<A[i]size();j++){

            cout<<setw(12)<<A[i][j]<<' ';

        }

        cout<<endl;

    }

    cout<<endl;

    vector<vector<double> > U;

    vector<double> S;

    vector<vector<double> > V;

    svd(A,k,U,S,V);

    cout<<"U="<<endl;

    for(int i=0;i<U[0]size();i++){

        for(int j=0;j<Usize();j++){

            cout<<setw(12)<<U[j][i]<<' ';

        }

        cout<<endl;

    }

    cout<<endl;

    cout<<"S="<<endl;

    for(int i=0;i<Ssize();i++){

        cout<<setw(7)<<S[i]<<' ';

    }

    cout<<endl;

    cout<<"V="<<endl;

    for(int i=0;i<V[0]size();i++){

        for(int j=0;j<Vsize();j++){

            cout<<setw(12)<<V[j][i]<<' ';

        }

        cout<<endl;

    }

    return 0;

}

SQL 数据库 实现递归查询的几种代码方法 表结构

ProductCategory

CategoryID Level ParentCategoryID

数据

T SQL

WITH CategoryTemp(CategoryID ParentCategoryID) 临时表用来保存查到的Category

(

SELECT CategoryID ParentCategoryID FROM ProductCategory WHERE ParentCategoryID<= 将所有的第一层查出来作为初始数据 需要查第几层或者哪个ParentCategoryID下面所有的 N层 把ParentCategoryID赋相关的值即可

UNION ALL 查询N层

SELECT pc CategoryID ParentCategoryID FROM ProductCategory pc

LEFT JOIN CategoryTemp ct ON pc ParentCategoryID=ct CategoryID

WHERE ParentCategoryID> 因为第一层前面已经查出来了 所以这里把第一层筛选掉

)

SELECT CategoryID ParentCategoryID FROM CategoryTemp

结果

如果把ParentCategoryID赋为 结果则为

实例

ID 是否为部门   部门名   上级ID        y                       部门             y                       部门             n                       张三              n                       李二              y                       部门             n                       王五              y                       部门3       n                       小三         我想找询   ID   值为      下级的所有人员包括下级部门的所有人员

创建查询函数 create   function   f_id( @id   int 要查询的id )returns   @re   table(id   int level   int) as begin declare   @l   int set   @l= insert   @re   select   id @l from   表   where   上级id=@id while   @@rowcount> begin set   @l=@l+ insert   @re   select   a id @l from   表   a   join   @re   b   on   a 上级id=b id   and   b level=@l end return end go

调用函数进行查询 select   a    from   表   a   join   f_id( )   b   on   a id=b id

联合查询

测试数据 create   table   表(ID   int 是否为部门   char( ) 部门名   varchar( ) 上级ID   int) insert   表   select      y 部门    union   all   select   y 部门    union   all   select   n 张三    union   all   select   n 李二    union   all   select   y 部门 union   all   select   n 王五    union   all   select   y 部门 union   all   select   n 小三    go

创建查询函数 create   function   f_id( @id   int 要查询的id )returns   @re   table(id   int level   int) as begin declare   @l   int set   @l= insert   @re   select   id @l from   表   where   上级id=@id while   @@rowcount> begin set   @l=@l+ insert   @re   select   a id @l from   表   a   join   @re   b   on   a 上级id=b id   and   b level=@l end return end go

调用函数进行查询 select   a    from   表   a   join   f_id( )   b   on   a id=b id go

删除测试 drop   table   表 drop   function   f_id

/ 测试结果

ID                     是否为部门   部门名                 上级ID                                                  n           小三                  

lishixinzhi/Article/program/MySQL/201311/29557

维基百科中的解释:

A database engine (or "storage engine") is the underlying software component that a database management system (DBMS) uses to create, retrieve, update and delete (see CRUD (acronym)) data from a database One may command the database engine via the DBMS's own user interface, and sometimes through a network port

数据库引擎(又称"存储引擎"),它是在数据库管理系统(DBMS)之下用来对数据库进行创建,检索,更新和删除等数据 *** 作一个组件数据库的使用者(数据库管理员DBA等)使用数据库管理系统(DBMS) *** 作发布指令,通过数据库引擎并连接网络发送给数据库

翻译得不是很好,见谅

回楼主:

现在有的引擎:

微软的:

Microsoft JET Database Engine

JAVA

JDBC

Borland

BDE

再答楼主:

你不用"我的天"了,因为知识是积累的,你一下子学这么多当然吃不消了,就看你现在学什么语言,就学那个就好了以后慢慢积累

再再回答楼主:

你的5分真难拿

每个公司开发语言,框架,当然都有一套自己的东西,引擎对于我们来说是抽象内容,我们只用他们提供的API来 *** 作,比如微软的引擎,我们就用ADO,ADONET来 *** 作

所以,

知识是累的

create table stuName

(stuName NCHAR(10) NOT NULL,

stuNo NVARCHAR(10) NOT NULL CHECK(stuNO LIKE 'S253%') UNIQUE,

stuSex NCHAR(1) NOT NULL CHECK(stuSex IN ('男','女')) DEFAULT '男' ,

stuAge tinyint NOT NULL CHECK (stuAge>=15 AND stuAge <=50),

stuSeat tinyint IDENTITY(1,1) CHECK(stuSeat <=30),

stuAddress nvarchar(100) NULL DEFAULT '地址不详'

)

下面的程序采用的步骤是,先把文本匡怡当中的数字分解到字符串数组中。然后调用第一个排序函数,对每一个分解出来的字符串进行内部的排序,使她按数字从小到大排列。这样就把123,321,132等等都变成了123。接着再调用第二个排序过程,把这些个字符串全部按照升序排列。最后把经过排序的字符串去重以后加入到文本框二中。下面是程序的代码和运行的结果。

Dim A() As String

Private Sub Command1_Click()

Text1 = "512 125 215 152 251 521 223 332 233 232 322 323"

A = Split(Text1, " ")

For i = 0 To UBound(A)

Call sort1(A(i))

Next i

Call sort2

i = 0

Do While i <= UBound(A)

Text2 = Text2 & A(i) & " "

i = i + 1

If i > UBound(A) Then Exit Do

Do While A(i) = A(i - 1)

i = i + 1

If i > UBound(A) Then Exit Do

Loop

Loop

End Sub

Private Sub Form_Load()

Text1 = ""

Text2 = ""

End Sub

Private Sub sort1(s As String)

If Mid(s, 1, 1) > Mid(s, 2, 1) Then t = Mid(s, 1, 1): Mid(s, 1, 1) = Mid(s, 2, 1): Mid(s, 2, 1) = t

If Mid(s, 1, 1) > Mid(s, 3, 1) Then t = Mid(s, 1, 1): Mid(s, 1, 1) = Mid(s, 3, 1): Mid(s, 3, 1) = t

If Mid(s, 2, 1) > Mid(s, 3, 1) Then t = Mid(s, 2, 1): Mid(s, 2, 1) = Mid(s, 3, 1): Mid(s, 3, 1) = t

End Sub

Private Sub sort2()

For i = 0 To UBound(A) - 1

For j = 0 To UBound(A) - 1 - i

If A(j) > A(j + 1) Then t = A(j): A(j) = A(j + 1): A(j + 1) = t

Next j

Next i

End Sub

以上就是关于求c++复数矩阵奇异值分解代码(svd)全部的内容,包括:求c++复数矩阵奇异值分解代码(svd)、SQL数据库实现递归查询的几种代码方法、database engine 是什么等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9666652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存