-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathGRAM_SCHMIDT_PROCESS.java
More file actions
89 lines (81 loc) · 1.63 KB
/
GRAM_SCHMIDT_PROCESS.java
File metadata and controls
89 lines (81 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//CODE FOR GRAM-SCHMIDT PROCESS WRITTEN BY Deep_Jani
import java.util.Scanner;
public class gram_schmidt4
{
static int n;
static int r;
static double sp(double[] a,double[] b)
{
double p=0;
for(int i=0;i<a.length;i++)
{
p=p+(a[i]*b[i]);
}
return p;
}
static double norm(double[] a)
{
double n=0;
for(int i=0;i<a.length;i++)
{
n=n+(a[i]*a[i]);
}
return n;
}
public static void main(String[] args)
{
//double[][] u={{1,1,1},{0,1,1},{0,0,1}};
Scanner dj =new Scanner(System.in);
System.out.println("Enter the dimension of co-ordinate space : ");
r=dj.nextInt();
System.out.println("Enter the number of elements in the basis : ");
n=dj.nextInt();
double[][] u=new double[n][r];
for(int i=0;i<n;i++)
{
for(int j=0;j<r;j++)
{
System.out.println("u"+i+1);
u[i][j]=dj.nextDouble();
}
}
double[][] v=new double[n][r];
double[][] a=new double[n][r];
///////////////////////////////////////////////////////////////
for(int i=0;i<r;i++)//V1=U1
{
v[0][i]=u[0][i];
}
for(int k=1;k<n;k++)//REST
{
for(int i=k;i<k+1;i++)
{
for(int j=0;j<=i;j++)
{
a[i][j]=(sp(u[i],v[j])/norm(v[j]));
}
}
for(int i=0;i<r;i++)
{
v[k][i]=u[k][i] ;
}
for(int j=0;j<k;j++)
{
for(int i=0;i<r;i++)
{
v[k][i]=v[k][i]- a[k][j]*v[j][i];
}
}
}
////////////////////////////////////////////////////////////////////////////
for(int i=0;i<n;i++)
{
for(int j=0;j<r;j++)
{
System.out.print(v[i][j]+" ");
}
System.out.println("");
}
////////////////////////////////////////////////////////////////////////////
}
}