Professional Documents
Culture Documents
Assignment 2
Assignment 2
Given a vector v in Rn space and a 1-based indices i and j where jth position is to be
placed with zero.
[ ]
1 0 …0
c s …0
The general form of a rotator matrix is G= : : : : where the cosine and sine are
−s c … 0
0 0 … 1
v [i] v [ j]
placed in ith and jth rows and columns. Here, c= and s=
√v [ i] +v [ j]
2 2
√v [i] +v[ j ]
2 2
6c) Output for [1 2 3]T that should have 0 in the 3rd position
6d) The Program for Givens Rotator Matrix in C++
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n, a, b;
float s, c;
cout<<"Enter the dimension of the space to be
considered:";
cin>>n;
float *arr = new float[n];
cout<<"Enter the "<<n<<" elements of the vector:";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter i and j (1-based):";
cin>>a>>b;
a-=1;
b-=1;
c = arr[a]/sqrt((arr[a]*arr[a])+(arr[b]*arr[b]));
s = arr[b]/sqrt((arr[a]*arr[a])+(arr[b]*arr[b]));
float **G = new float*[n];
for(int i=0;i<n; i++)
{
G[i] = new float[n];
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==a && j==a)
{
G[i][j] = c;
}
else if(i==a && j==b)
{
G[i][j] = s;
}
else if(i==b && j==a)
{
G[i][j] = -1*s;
}
else if(i==b && j==b)
{
G[i][j] = c;
}
else if(i==j)
{
G[i][j] = 1;
}
else
{
G[i][j] = 0;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<G[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
6e) Output for a R10 vector to make zero in 8th position w.r.t 6th
7a) Householder Reflector Matrix Algorithm
Given a vector v in Rn space and a 1-based index r from which zeros are to be placed.
2
The general formula to compute Householder reflector matrix H is H¿ I − T
( u uT )
u u
We shall prepare a vector u = x-y where x = [x1, x2, …xn-r+1]T where xi = vi+r-1 and y =
√∑
n−r +1
[y1, y2, …yn-r+1] where y 1= x i and y i=0 ∀ i≥ 2.
2
i =1
7c) Output for [1 2 3]T that should have 0 in the 3rd position
7e) Output for a R10 vector to make zeros from 6th position