I'm doing LU decom and I found this code on googel ,but want to understan it by output 'pvt' and' a 'but it semes my pvt is not correct so I got something diffrent so pease could any one correct me ..
Thanks
here is my code
int* LUfactor ( double **a, int n, int ps )
/*PURPOSE: compute an LU decomposition for the coefficient matrix a
CALLING SEQUENCE:
pvt = LUfactor ( a, n, ps );
INPUTS:
a coefficient matrix
type: **doble
n number of equations in system
type: int
ps flag indicating which pivoting strategy to use
ps == 0: no pivoting
ps == 1; partial pivoting
ps == 2; scaled partial pivoting
type: int
OUTPUT:
pvt vector which indicates the permutation of the rows
performed during the decomposition process
type: *int
a matrix containing LU decomposition of the input coefficient
matrix - the L matrix in the decomposition consists of 1's
along the main diagonal together with the strictly lower
triangular portion of the output matrix a; the U matrix
in the decomposition is theupper triangular portion of the
output matrix a
type: **double
*/
{
int pass, row, col, *pvt, j, temp;
double *s,rmax,ftmp, mult, sum;
/*initialize row pointer array*/
pvt = new int [n];
for ( row = 0; row < n; row++ )
pvt[row] = row;
/* if scaled partial pivoting option was selected,
initialize scale vector*/
if ( ps == 2 ) {
s = new double [n];
for ( row = 0; row < n; row++ ) {
s[row] = fabs( a[row][0] );
for ( col = 1; col < n; col++ )
if ( fabs( a[row][col] ) > s[row] )
s[row] = fabs( a[row][col] );
}
}
/*elimination phase*/
for ( pass = 0; pass < n; pass++ ) {
/* perform requested pivoting strategy
even if no pivoting option is requested, still must check for
zero pivot*/
if ( ps != 0 ) {
rmax = ( ps == 1 ? fabs( a[pvt[pass]][pass] ) :
fabs( a[pvt[pass]][pass] ) / s[pvt[pass]] );
j = pass;
for ( row = pass+1; row < n; row++ ) {
ftmp = ( ps == 1 ? fabs( a[pvt[row]][pass] ) :
fabs( a[pvt[row]][pass] ) / s[pvt[row]] );
if ( ftmp > rmax ) {
rmax = ftmp;
j = row;
}
}
if ( j != pass ) {
temp = pvt[j];
pvt[j] = pvt[pass];
pvt[pass] = temp;
}
}
else {
if ( a[pvt[pass]][pass] == 0.0 ) {
for ( row = pass+1; row < n; row++ )
if ( a[pvt[row]][pass] != 0.0 ) break;
temp = pvt[row];
pvt[row] = pvt[pass];
pvt[pass] = temp;
}
}
for ( row = pass + 1; row < n; row++ ) {
mult = - a[pvt[row]][pass] / a[pvt[pass]][pass];
a[pvt[row]][pass] = -mult;
for ( col = pass+1; col < n; col++ )
a[pvt[row]][col] += mult * a[pvt[pass]][col];
}
}
if ( ps == 2 ) delete [] s;
return ( pvt );
}
Here is my main
double **af;
int *pvt;
int i, j, n;
/*
allocate space for coefficient matrix
*/
n = 4;
af = new double* [n];
pvt = new int [n];
for ( i = 0; i < n; i++ )
af[i] = new double [n];
af[0][0] = 2.00; af[0][1] = 1.00; af[0][2] = 1.00; af[0][3] = -2.00;
af[1][0] = 4.00; af[1][1] = 0.00; af[1][2] = 2.00; af[1][3] = 1.00;
af[2][0] = 3.00; af[2][1] = 2.00; af[2][2] = 2.00; af[2][3] = 0.00;
af[3][0] = 1.00; af[3][1] = 3.00; af[3][2] = 2.00; af[3][3] = 0.00;
pvt =LUfactor ( af, n, 0 );
cout << "pvt" << endl;
for ( i = 0; i < n; i++ )
cout << pvt[i] << endl;
cout << endl << endl << endl;
cout << "a" << endl;
for ( i = 0; i < n; i++ )
cout << af[i][i] << endl;
cout << endl << endl << endl;
///////
out put
pvt
0
3
1
2
LU matrix is
2 1 1 -2 0
2 -0.8 1.2 5.8 0
1.5 0.2 0.166667 1.83333 0
0.5 2.5 1.5 1 0
Segmentation fault
////////////////////////////////////////
The out put I'm looking for is
Matrix A
0 2 0 1
2 2 3 2
4 -3 0 1
6 1 -6 -5
determinant: -234
pivot vector: 3 2 1 0
Lower triangular matrix
6 0 0 0
4 -3.667 0 0
2 1.667 6.818 0
0 2 2.182 1.56
Upper triangular matrix
1 0.1667 -1 -0.8333
0 1 -1.091 -1.182
0 0 1 0.8267
0 0 0 1
Product of L U
6 1 -6 -5
4 -3 0 1
2 2 3 2
0 2 0 1
Right-hand-side number 1
0.0000 -2.0000 -7.0000 6.0000
Solution vector
-0.5000 1.0000 0.3333 -2.0000
You didn't read the fine documentation. It clearly says
CALLING SEQUENCE:
pvt = LUfactor ( a, n, ps );
You used the function incorrectly. You allocated and populated pvt, and then you ignored the return value from LUfactor. You do not allocate pvt; the function LUfactor does. You need to call LUfactor per the documentation.
Related
I'm trying to transform the adjacency matrix into an incidence matrix of an undirected graph. For edges :
(1, 2), (1,5), (1,6), (2,3), (2,5), (3,4), (3,5), (4,5), (5,6)
Adj matrix is :
0 1 0 0 1 1
1 0 1 0 1 0
0 1 0 1 1 0
0 0 1 0 1 0
1 1 1 1 0 1
1 0 0 0 1 0
and I expect the result for the incidence matrix to be
0 1 0 0 1 1 0 0 0
1 0 1 0 1 0 0 0 0
0 1 0 1 1 0 0 0 0
0 0 1 0 1 0 0 0 0
1 1 1 1 0 1 0 0 0
1 0 0 0 1 0 0 0 0
but, my program returns this :
1 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0
0 1 0 1 1 0 0 0 0
0 0 0 1 0 1 0 0 0
1 0 1 0 1 1 0 0 0
0 0 0 0 0 0 0 0 0
My source code :
graph constructor
Graph(int vertices, int edges)
{
this->vertices = vertices;
this->edges = edges;
edge = std::vector<Graph::Edge*>(edges);
for (int i = 0; i < edges; i++)
{
edge[i] = new Edge(this);
}
}
Graph* g = new Graph(numberOfVertices, numberOfEdges);
g->edge[0]->src = 1;
g->edge[0]->dest = 2;
g->edge[1]->src = 1;
g->edge[1]->dest = 5;
g->edge[2]->src = 1;
g->edge[2]->dest = 6;
g->edge[3]->src = 2;
g->edge[3]->dest = 3;
g->edge[4]->src = 2;
g->edge[4]->dest = 5;
g->edge[5]->src = 3;
g->edge[5]->dest = 4;
g->edge[6]->src = 3;
g->edge[6]->dest = 5;
g->edge[7]->src = 4;
g->edge[7]->dest = 5;
g->edge[8]->src = 5;
g->edge[8]->dest = 6;
for (i = 0; i < numberOfEdges; i++)
{
adjacency_matrix[g->edge[i]->src][g->edge[i]->dest] = 1;
adjacency_matrix[g->edge[i]->dest][g->edge[i]->src] = 1;
}
std::cout << "Adjacency matrix : " << std::endl;
for (i = 1; i <= numberOfVertices; i++)
{
for (j = 1; j <= numberOfVertices; j++)
{
std::cout<<adjacency_matrix[i][j]<<" ";
}
std::cout << std::endl;
}
// Incidence Matrix
int counter = 0;
for( int i = 1; i <= numberOfEdges; i++){
for(int j = i + 1; j < numberOfVertices; j++ ){
if(adjacency_matrix[i][j] == 1){
incidence_matrix[i][counter] = 1;
incidence_matrix[j][counter] = 1;
++counter;
}
}
}
for( int i = 1; i <= numberOfVertices; i++){
for(int j = 1; j <= numberOfEdges; j++){
std::cout<<incidence_matrix[i][j]<<" ";
}
std::cout<<std::endl;
}
The ideas in the code are correct. But the indexing in the array is wrong.
Indexing should start at 0. Note: this also applies when setting up the adjacency matrix.
The numbers you use to name the vertices/nodes where originally 1,2,3,4,5,6. I propose to call them 0,1,2,3,4,5. Your original edge (1,2) then becomes (0,1). But if we consistently rename all the vertices everywhere we end up with the same graph. The advantage of this new naming convention is that we can use these names directly as indices in the C++ data structures you are using. (Provided we use the corresponding integer value and not consider these names to be strings.)
The specification of the Graph becomes
Graph* g = new Graph(numberOfVertices, numberOfEdges);
g->edge[0]->src = 0;
g->edge[0]->dest = 1;
g->edge[1]->src = 0;
g->edge[1]->dest = 4;
g->edge[2]->src = 0;
g->edge[2]->dest = 5;
g->edge[3]->src = 1;
g->edge[3]->dest = 2;
g->edge[4]->src = 1;
g->edge[4]->dest = 4;
g->edge[5]->src = 2;
g->edge[5]->dest = 3;
g->edge[6]->src = 2;
g->edge[6]->dest = 4;
g->edge[7]->src = 3;
g->edge[7]->dest = 4;
g->edge[8]->src = 4;
g->edge[8]->dest = 5;
So printing the adjacency matrix becomes:
std::cout << "Adjacency matrix : " << std::endl;
for (i = 0; i < numberOfVertices; i++)
{
for (j = 0; j < numberOfVertices; j++)
{
std::cout<<adjacency_matrix[i][j]<<" ";
}
std::cout << std::endl;
}
and the calculation of the incidence matrix becomes:
// Incidence Matrix
int counter = 0;
for( int i = 0; i < numberOfVertices; i++){ //numberOfVertices!!
for(int j = i + 1; j < numberOfVertices; j++ ){
if(adjacency_matrix[i][j] == 1){
incidence_matrix[i][counter] = 1;
incidence_matrix[j][counter] = 1;
++counter;
}
}
}
for( int i = 0; i < numberOfVertices; i++){
for(int j = 0; j < numberOfEdges; j++){
std::cout<<incidence_matrix[i][j]<<" ";
}
std::cout<<std::endl;
}
Note that the order of the edges is determined now by the order in which you traverse the adjacency matrix.
There is a wall built from numbers. 0 means there is a hole and blocks can't sit on holes. Someone has a special gun that fires all blocks with a number in one shot.
So I have a matrix called wall and have to write a gun. I wrote the program, but I have a problem and I do not understand why it is happening. In my code
#include <iostream>
#include <cstdio>
using namespace std;
int createWall( int &height, int &length, int wall[][ 100 ], int shots )
{
int i;
int j;
cin >> height;
cin >> length;
cin >> shots;
for ( i = 0; i < height; i++ )
{
for ( j = 0; j < length; j++ )
{
cin >> wall[ i ][ j ];
}
}
return shots;
}
void wallNow( int height, int length, int wall[][ 100 ] )
{
int i;
int j;
for ( i = 0; i < height; i++ )
{
for ( j = 0; j < length; j++ )
{
cout << wall[ i ][ j ] << " ";
}
cout << "\n";
}
}
void destroyWall( int height, int length, int wall[][100], int shots )
{
int i;
int j;
int k;
int x;
int aimedBlocks;//number to be "destroyed"
//set all aimedBlocks to 0
for ( x = 0; x < shots; x++ )
{
cin >> aimedBlocks;
for ( i = 0; i < height; i++ )
{
for ( k = 0; k < length; k++ )
{
if ( wall[ i ][ k ] == aimedBlocks )
{
wall[ i ][ k ] = 0;
}
}
}
}
int counter;//I use this variable because at some point I have a 0 followed only by 0's
for ( i = 0; i < length; i++ )
{
j = height - 1;
counter = 0;
//if I find a 0 then I move all elements higher that it one step down
while ( counter < height )
{
if ( wall[ j ][ i ] == 0 )
{
for ( k = j; k > 0; k-- )
{
wall[ k ][ i ] = wall[ k - 1 ][ i ];
}
wall[ height - j - 1 ][ i ] = 0;
}
else
j--;//I don't always go up ene step because the "block" droped in place of 0 may be 0
counter++;
}
}
}
int main()
{
int height;
int length;
int wall[ 100 ][ 100 ];
int shots = 0;
shots = createWall( height, length, wall, shots );
destroyWall( height, length, wall, shots );
wallNow( height, length, wall );
}
I really do not understand why line wall[ height - j - 1 ][ i ] = 0; is working for the first 4 columns in the following example and it does not work for the last one.
Format input:
height length shots
wall_0_0 ... wall_0_length
... ... ...
wall_height ... wall_height_length
shot_0 ... shot_shots
Input:
4 5 3
3 5 4 5 1
2 1 1 5 3
1 1 5 5 1
5 5 1 4 3
1 5 1
Remove all values that matches with 1, 5, 1. And wall remains must drop into the bottom.
Output:
0 0 0 0 0
0 0 0 0 0
3 0 0 0 0
2 0 4 4 3
Expected:
0 0 0 0 0
0 0 0 0 0
3 0 0 0 3
2 0 4 4 3
Please help me solve this problem. I could not find it debugging the code.
Your algorithm is strange, I don't understand what you try to do.
A simple way to achieve your purpose is to iterate from the left to the right of your wall, then for each you iterate from the bottom to the top. Each time you get a 0, you search for a non zero value to the top and swap their if you found it.
Example (very basic could be improve):
for (size_t i = 0; i < length; i++) { // i is for iterate from left(0) to right(length - 1)
size_t j = height; // j is for iterate from bot(height - 1) to top(0)
while (j-- > 0) {
if (wall[j][i] == 0) {
size_t k = j; // k is for found a non zero value from j - 1 to the top(0)
while (k-- > 0) {
if (wall[k][i] != 0) {
wall[j][i] = wall[k][i];
wall[k][i] = 0;
break;
}
}
}
}
}
Note:
I use size_t because this is the type for index.
I recommend you to switch for std::vector and use iterator on it in C++.
I want to implement ,as possible , spdiags function in C.
(I prefer C to C++ and I don't want to use C++ algorithms for now)
Having as input matrix:
inMx =
1 0 0
4 5 6
0 7 9
you should obtain (running that using spdiags in Matlab) :
ouMx =
4 1 0
7 5 0
0 9 6
(one thing I can't understand though , is even though at the documentation it says that zeros are inserted at the top if you are below the main diagonal , here you we can see that happens the opposite , but in the example in the link it's ok )
With my code below , I am taking as output:
ouMx =
4 7 0
7 5 9
0 9 6
so I think I am close!
I zeroed out the output matrix instead of having to insert zeros at the bottom or top of columns.
But I can't completeley handled the above/below the main diagonal.
I used if ( j > i ) swap rows , but it doesn't work , so I just use swap rows.
( I am assuming square matrix for this example ,but it should work for any matrix )
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void Diag( int Rows , int Cols , float * inMx , float * ouMx );
void swapRows( int Rows , int Cols , float * Mx );
int main( int argc, const char* argv[] ){
int Rows = 3 , Cols = 3;
float *inMx = (float *) malloc ( Rows * Cols * sizeof (float) );
float *ouMx = (float *) malloc ( Rows * Cols * sizeof (float) );
// assume row major order
inMx[0] = 1.0;
inMx[1] = 0.0;
inMx[2] = 0.0;
inMx[3] = 4.0;
inMx[4] = 5.0;
inMx[5] = 6.0;
inMx[6] = 0.0;
inMx[7] = 7.0;
inMx[8] = 9.0;
// print input matrix ( row major )
printf("\n Input matrix \n\n");
for ( int i = 0; i < Rows; i++ )
for ( int j = 0; j < Cols; j++ ) {
printf("%f\t",inMx[ i * Cols + j ]);
if( j == Cols-1 )
printf("\n");
}
printf("\n");
// extract diagonals
Diag( Rows , Cols , inMx , ouMx );
// print Diagonal matrix
printf("\n Diagonal matrix \n\n");
for ( int i = 0; i < Rows; i++ )
for (int j = 0; j < Cols; j++ ) {
printf("%f\t",ouMx[ i * Cols + j ]);
if( j == Cols-1 )
printf("\n");
}
printf("\n");
free( inMx );
free( ouMx );
return 0;
}
void Diag( int Rows , int Cols , float * inMx , float * ouMx )
{
//zero out ouMx
memset( ouMx , 0 , Rows * Cols * sizeof(float) );
// scan from the last line to the first -1 for each column
for ( int j = 0; j < Cols; j++ )
{
for ( int i = ( Rows - 1 ); i > 0 ; i-- )
{
// neglect the zero elements
if ( inMx[ i * Cols + j ] != 0 )
{
ouMx[ i * Cols + j ] = inMx[ i * Cols + j ];
//if the element in the next colulmn is !=0
if ( inMx[ ( i + 1 ) * Cols + ( j + 1 ) ] != 0 )
{
ouMx[ ( i + 1 ) * Cols + j ] = inMx[ ( i + 1 ) * Cols + ( j + 1 ) ];
}
}
//if we are above the main diagonal
//swap elements of a row (in each column) in order to have the zeros at bottom/top
// if ( i > j ) doesn't work
swapRows( Rows , Cols , ouMx );
}
}
}
void swapRows( int Rows , int Cols , float * Mx )
{
float temp;
for ( int j = 0; j < Cols; j++ )
{
for ( int i = ( Rows - 1 ); i > 0 ; i-- )
{
temp = Mx[ ( i - 1 ) * Cols + j ];
Mx[ ( i - 1 ) * Cols + j ] = Mx[ i * Cols + j ];
Mx[ i * Cols + j ] = temp;
}
}
}
Program: So I made a program that take two numbers, N and L. N is the size of a 2D array and L is a number from 3 - 16. The program builds the array and starts at the center and works its way out in a counter clockwise spiral. I is the value of the center and its as you go through the array( in the spiral ) the value will increase by one. It it is prime, that number will be assigned to that spot and if not it * will take its place instead.
Error: I'm getting a "Floating point exception " error, how would I solve this?
Code:
void Array_Loop( int *Array, int n, int L ) ;
int Is_Prime( int Number ) ;
int main( int argc, char *argv[] ){
int **Array ;
int n, L ;
n = atoi( argv[1] ) ;
L = atoi( argv[2] ) ;
Matrix_Build( &Array, n, n ) ;
Array_Loop( Array, n, L ) ;
return 0 ;
}
void Array_Loop( int *Array, int n, int L ){
int i, j, k, h ;
int lctn, move;
lctn = n / 2 + 1 ;
i = lctn ;
j = lctn ;
move = 1
while( i != 0 && j != n ){
for( j = lctn ; j < lctn + move ; j++ ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move = move * -1 ;
for( i = i ; i > lctn - move ; i-- ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move-- ;
for( j = j ; j > lctn - move ; j-- ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move = move * -1 ;
for( i = i ; i < lctn - move ; i-- ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move++ ;
}
}
int Is_Prime( int Number ){
int i ;
for( i = 0 ; i < Number / 2 ; i++ ){
if( Number % i != 0 ) return -1 ;
}
return Number ;
}
You are getting Floating point exception because Number % i, when i is 0:
int Is_Prime( int Number ){
int i ;
for( i = 0 ; i < Number / 2 ; i++ ){
if( Number % i != 0 ) return -1 ;
}
return Number ;
}
Just start the loop at i = 2. Since i = 1 in Number % i it always be equal to zero, since Number is a int.
Floating Point Exception happens because of an unexpected infinity or NaN.
You can track that using gdb, which allows you to see what is going on inside your C program while it runs. For more details:
https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_gdb.php
In a nutshell, these commands might be useful...
gcc -g myprog.c
gdb a.out
gdb core a.out
ddd a.out
I got a bit stuck with my algorithm and I need some help to solve my problem. I think an example would explain better my problem.
Assuming:
d = 4 (maximum number of allowed bits in a number, 2^4-1=15).
m_max = 1 (maximum number of allowed bits mismatches).
kappa = (maximum number of elements to find for a given d and m, where m in m_max)
The main idea is for a given number, x, to compute its complement number (in binary base) and all the possible combinations for up to m_max mismatches from x complement's number.
Now the program start to scan from i = 0 till 15.
for i = 0 and m = 0, kappa = \binom{d}{0} = 1 (this called a perfect match)
possible combinations in bits, is only 1111 (for 0: 0000).
for i = 0 and m = 1, kappa = \binom{d}{1} = 4 (one mismatch)
possible combinations in bits are: 1000, 0100, 0010 and 0001
My problem was to generalize it to general d and m. I wrote the following code:
#include <stdlib.h>
#include <iomanip>
#include <boost/math/special_functions/binomial.hpp>
#include <iostream>
#include <stdint.h>
#include <vector>
namespace vec {
typedef std::vector<unsigned int> uint_1d_vec_t;
}
int main( int argc, char* argv[] ) {
int counter, d, m;
unsigned num_combination, bits_mask, bit_mask, max_num_mismatch;
uint_1d_vec_t kappa;
d = 4;
m = 2;
bits_mask = 2^num_bits - 1;
for ( unsigned i = 0 ; i < num_elemets ; i++ ) {
counter = 0;
for ( unsigned m = 0 ; m < max_num_mismatch ; m++ ) {
// maximum number of allowed combinations
num_combination = boost::math::binomial_coefficient<double>( static_cast<unsigned>( d ), static_cast<unsigned>(m) );
kappa.push_back( num_combination );
for ( unsigned j = 0 ; j < kappa.at(m) ; j++ ) {
if ( m == 0 )
v[i][counter++] = i^bits_mask; // M_0
else {
bit_mask = 1 << ( num_bits - j );
v[i][counter++] = v[i][0] ^ bits_mask
}
}
}
}
return 0;
}
I got stuck in the line v[i][counter++] = v[i][0] ^ bits_mask since I was unable to generalize my algorithm to m_max>1, since I needed for m_max mismatches m_max loops and in my original problem, m is unknown until runtime.
i wrote a code that do what i want, but since i am newbie, it is a bit ugly.
i fixed m and d although this code would work fine for genral m and d.
the main idea is simple, assuming we would like to compute the complement of 0 up to two failure (d= 4,m=2), we will see that max number of possibilities is given by \sum_{i = 0)^2\binom{4}{i} = 11.
The complement to 0 (at 4 bits) is 15
With 1 bit possible mismatch (from 15): 7 11 13 14
with 2 bits possible mismatches (from 15): 3 5 6 9 10 12
i wanted that the output of this program will be a vector with the numbers 15 7 11 13 14 3 5 6 9 10 12 inside it.
i hope that this time i am more clear with presenting my question (although i also supplied the solution). I would appreachiate if someone could point out, in my code, ways to improve it and make it faster.
regards
#include <boost/math/special_functions/binomial.hpp>
#include <iostream>
#include <vector>
#define USE_VECTOR
namespace vec {
#if defined(USE_VECTOR) || !defined(USE_DEQUE)
typedef std::vector<unsigned int> uint_1d_vec_t;
typedef std::vector<uint_1d_vec_t> uint_2d_vec_t;
#else
typedef std::deque<unsigned int> uint_1d_vec_t;
typedef std::deque<uint_1d_vec_t> uint_2d_vec_t;
#endif
}
using namespace std;
void get_pointers_vec( vec::uint_2d_vec_t &v , unsigned num_elemets , unsigned max_num_unmatch , unsigned num_bits );
double get_kappa( int m , int d );
int main( ) {
unsigned int num_elements , m , num_bits;
num_elements = 16;
num_bits = 4; // 2^4 = 16
m = 2;
double kappa = 0;
for ( unsigned int i = 0 ; i <= m ; i++ )
kappa += get_kappa( num_bits , i );
vec::uint_2d_vec_t Pointer( num_elements , vec::uint_1d_vec_t (kappa ,0 ) );
get_pointers_vec( Pointer , num_elements , m , num_bits );
for ( unsigned int i = 0 ; i < num_elements ; i++ ) {
std::cout << setw(2) << i << ":";
for ( unsigned int j = 0 ; j < kappa ; j++ )
std::cout << setw(3) << Pointer[i][j];
std::cout << std::endl;
}
return EXIT_SUCCESS;
}
double get_kappa( int n , int k ) {
double kappa = boost::math::binomial_coefficient<double>( static_cast<unsigned>( n ), static_cast<unsigned>(k) );
return kappa;
}
void get_pointers_vec( vec::uint_2d_vec_t &v , unsigned num_elemets , unsigned max_num_unmatch , unsigned num_bits ) {
int counter;
unsigned num_combination, ref_index, bits_mask, bit_mask;
vec::uint_1d_vec_t kappa;
bits_mask = pow( 2 , num_bits ) - 1;
for ( unsigned i = 0 ; i < num_elemets ; i++ ) {
counter = 0;
kappa.clear();
ref_index = 0;
for ( unsigned m = 0 ; m <= max_num_unmatch ; m++ ) {
num_combination = get_kappa( num_bits , m ); // maximum number of allowed combinations
kappa.push_back( num_combination );
if ( m == 0 ) {
v[i][counter++] = i^bits_mask; // M_0
}
else if ( num_bits == kappa.at(m) ) {
for ( unsigned k = m ; k <= num_bits ; k++ ) {
bit_mask = 1 << ( num_bits - k );
v[i][counter++] = v[i][ref_index] ^ bit_mask;
}
}
else {
// Find first element's index
ref_index += kappa.at( m - 2 );
for( unsigned j = 0 ; j < ( kappa.at(m - 1) - 1 ) ; j++ ) {
for ( unsigned k = m + j ; k <= num_bits ; k++ ) {
bit_mask = 1 << ( num_bits - k );
v[i][counter++] = v[i][ref_index] ^ bit_mask;
}
ref_index++;
}
}
}
}
}