-
Notifications
You must be signed in to change notification settings - Fork 0
/
subspaces.cpp
44 lines (43 loc) · 1.35 KB
/
subspaces.cpp
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
//#include "vect.cpp"
//#include "vect_util.cpp"
#include "gram_schmidt.cpp"
#include <vector>
//it's assumed that everything is a basis
template<typename T,int n>
vector<vect<T,n> > intersect_subspaces(vector<vect<T,n> >A,
vector<vect<T,n> > B){
//calculate both perp spaces, then the A\cap B is the perp space of the
//sum of the perp spaces
vector<vect<T,n> > perpA,perpB;
for(int i=0;i<n;i++){
vect<T,n> temp1,temp2;
temp1[i]=1;
temp2[i]=1;
for(int j=0;j<A.size();j++)
temp1 = orth_proj(temp1,A[j]);
for(int j=0;j<B.size();j++)
temp2 = orth_proj(temp2,B[j]);
perpA.push_back(temp1);
perpB.push_back(temp2);
if(!gram_schmidt(perpA,perpA.size(),false))
perpA.pop_back();
if(!gram_schmidt(perpB,perpB.size(),false))
perpB.pop_back();
}
for(int i=0;i<perpB.size();i++){
perpA.push_back(perpB[i]);
if(!gram_schmidt(perpA,perpA.size(),false))
perpA.pop_back();
}
vector<vect<T,n> >ret;
for(int i=0;i<n;i++){
vect<T,n> temp;
temp[i]=1;
for(int j=0;j<perpA.size();j++)
temp = orth_proj(temp,perpA[j]);
ret.push_back(temp);
if(!gram_schmidt(ret,ret.size(),false))
ret.pop_back();
}
return ret;
}