Non-singular matrix Lu decomposition C ++ Program

Source: Internet
Author: User

// Lluu. cpp
// Lu decomposition of the matrix
# Include <iostream>
# Include <fstream>
# Include <cmath>
Using namespace STD;
Class lluu
{
PRIVATE:
Int N;
Double ** A, ** L, ** U;
Public:
Lluu (int nn)
{
Int I;
N = nn;
A = new double * [N];
For (I = 0; I <n; I ++) A [I] = new double [n + 8];
L = new double * [N];
For (I = 0; I <n; I ++) L [I] = new double [n + 8];
U = new double * [N];
For (I = 0; I <n; I ++) U [I] = new double [n + 8];
}
Void input ();
Void Lu ();
Void output ();
~ Lluu ()
{
Int I;
For (I = 0; I <n; I ++) {Delete [] a [I];}
Delete [];
For (I = 0; I <n; I ++) {Delete [] L [I];}
Delete [] L;
For (I = 0; I <n; I ++) {Delete [] U [I];}
Delete [] U;
}
};

Void lluu: input ()
{
Int I, J;
Char str1 [20];
Cout <"input file name :";
Cin> str1;
Ifstream fin (str1 );
If (! Fin)
{Cout <"\ n cannot open this file" <str1 <Endl; exit (1 );}
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
Fin> A [I] [J];
Fin. Close ();
}

Void lluu: Lu ()
{
Int I, J, K;
For (k = 0; k <n-1; k ++)
{
For (I = k + 1; I <n; I ++)
{
A [I] [k] = A [I] [k]/A [k] [k];
For (j = k + 1; j <n; j ++)
A [I] [J] = A [I] [J]-A [I] [k] * A [k] [J];
}
}
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
If (j <I) {L [I] [J] = A [I] [J]; U [I] [J] = 0 ;}
If (j = I) {L [I] [J] = 1; U [I] [J] = A [I] [J];}
If (j> I) {L [I] [J] = 0; U [I] [J] = A [I] [J];}
}
}
}

Void lluu: output ()
{
Int I, J;
Char str2 [20];
Cout <"output file name :";
Cin> str2;
Ofstream fout (str2 );
If (! Fout)
{Cout <"\ n cannot open this file" <str2 <Endl; exit (1 );}
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{Fout <"<L [I] [J];
Cout <"<L [I] [J];
}
Fout <Endl; cout <Endl;
}
Fout <Endl; cout <Endl;
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{Fout <"<u [I] [J];
Cout <"<u [I] [J];
}
Fout <Endl; cout <Endl;
}
Fout. Close ();
}

Int main ()
{
Lluu C (4 );
C. Input ();
C. Lu ();
C. Output ();
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.