Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

6a) Computation of Givens Rotator Algorithm

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

Now, the rotator G is computed using the following algorithm:


c = v[a]/sqrt((v[a]*v[a])+(v[b]*v[b]))
s = v[b]/sqrt((v[a]*v[a])+(v[b]*v[b]))
For i=1 to n:
For j=1 to n:
If i==a and j==a
G[i][j] = c
Else if i==a and j==b
G[i][j] = s
Else if i==b and j==a
G[i][j] = -1*s
Else if i==b and j==b
G[i][j] = c
Else if i==j
G[i][j] = 1
Else
G[i][j] = 0

6b) Number of Operations


For computing the givens rotator matrix, the algorithm uses at most two loops i.e.,
repetitive control structures each of n iterations. Thus, the number of operations will be
a total order of n2 floating-point operations.

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

Now, H is computed using the following algorithm:


For i=1 to n-r:
For j=1 to n-r:
2
uut[i][j] = -1* T *u[i]*u[j];
u u
If i==j:
uut[i][j]+=1

7b) Number of Operations


For computing the householder reflector matrix, the algorithm uses at most two loops
i.e., repetitive control structures each of n iterations. Thus, the number of operations
will be a total order of n2 floating-point operations.

7c) Output for [1 2 3]T that should have 0 in the 3rd position

7d) The Program for Householder Reflector Matrix in C++


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n, r;
float mag = 0, denominator = 0;
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 the index (1-based) from which the vector
should have zeros:";
cin>>r;
r-=2;
float *u = new float[n-r];
for(int i=0;i<n-r;i++)
{
u[i] = arr[i+r];
mag = mag + (arr[i+r])*(arr[i+r]);
}
u[0] -= sqrt(mag);
for(int i=0;i<n-r;i++)
denominator += (u[i]*u[i]);
denominator = 2.0f/denominator;
float **uut = new float*[n-r];
for(int i=0;i<n-r; i++)
uut[i] = new float[n-r];
for(int i=0;i<n-r;i++)
{
for(int j=0;j<n-r;j++)
uut[i][j] = -1*denominator*u[i]*u[j];
}
for(int i=0;i<n-r;i++)
uut[i][i] = 1+uut[i][i];
float **h = new float*[n];
for(int i=0;i<n; i++)
h[i] = new float[n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i>=r && j>=r)
h[i][j] = uut[i-r][j-r];
else if(i==j)
h[i][j] = 1;
else
h[i][j] = 0;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<h[i][j]<<" ";
cout<<endl;
}
return 0;
}

7e) Output for a R10 vector to make zeros from 6th position

You might also like