From 396de3715ed2615f81325661c264341165f0edb2 Mon Sep 17 00:00:00 2001 From: siddhu8990 Date: Tue, 14 Jun 2016 12:30:32 +0530 Subject: Support added for diff, norm functions --- src/c/matrixOperations/norm/dnorma.c | 72 ++++++++++++++++++++++++++++++++++++ src/c/matrixOperations/norm/dnormv.c | 60 ++++++++++++++++++++++++++++++ src/c/matrixOperations/norm/snorma.c | 72 ++++++++++++++++++++++++++++++++++++ src/c/matrixOperations/norm/snormv.c | 60 ++++++++++++++++++++++++++++++ 4 files changed, 264 insertions(+) create mode 100644 src/c/matrixOperations/norm/dnorma.c create mode 100644 src/c/matrixOperations/norm/dnormv.c create mode 100644 src/c/matrixOperations/norm/snorma.c create mode 100644 src/c/matrixOperations/norm/snormv.c (limited to 'src/c/matrixOperations/norm') diff --git a/src/c/matrixOperations/norm/dnorma.c b/src/c/matrixOperations/norm/dnorma.c new file mode 100644 index 0000000..c912f85 --- /dev/null +++ b/src/c/matrixOperations/norm/dnorma.c @@ -0,0 +1,72 @@ + /* Copyright (C) 2016 - IIT Bombay - FOSSEE + + This file must be used under the terms of the CeCILL. + This source file is licensed as described in the file COPYING, which + you should have received as part of this distribution. The terms + are also available at + http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt + Author: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + + /*This function calculates norms for matrices*/ + /*Acceptable norms are: 1, 2, 'inf', 'fro', */ + +#include "norm.h" +#include + +double dnorma (double *in, int row, int col, int norm) +{ + double res = 0, sum = 0; + int col_count, row_count; + + switch (norm) + { + case 1: /*largest column sum*/ + for(col_count = 0; col_count < col; col_count++) + { + sum = 0; + for(row_count = 0; row_count < row; row_count++) + { + sum += in[col_count*row+row_count]; + } + if (sum >= res) + res = sum; + } + break; + + case 2: /*Largest singular value of the matrix*/ + break; + + case 3: /*inf: largest row sum*/ + + for(row_count = 0; row_count < row; row_count++) + { + sum = 0; + for(col_count = 0; col_count < col; col_count++) + { + sum += in[col_count*row + row_count]; + } + if(sum >= res) + res = sum; + } + break; + + case 4: /*Frobenius norm*/ + + for(row_count = 0; row_count < row; row_count++) + { + for(col_count = 0; col_count < col; col_count++) + { + sum += in[col_count*row + row_count] * in[col_count*row + row_count]; + } + } + res = sqrt(sum); + + break; + } + + + return res; +} \ No newline at end of file diff --git a/src/c/matrixOperations/norm/dnormv.c b/src/c/matrixOperations/norm/dnormv.c new file mode 100644 index 0000000..5d20d60 --- /dev/null +++ b/src/c/matrixOperations/norm/dnormv.c @@ -0,0 +1,60 @@ + /* Copyright (C) 2016 - IIT Bombay - FOSSEE + + This file must be used under the terms of the CeCILL. + This source file is licensed as described in the file COPYING, which + you should have received as part of this distribution. The terms + are also available at + http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt + Author: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + + /*This function calculates norms for vectors (row or column)*/ + +#include "norm.h" +#include + +double dnormv (double *in, int size, int norm) +{ + double sum = 0; + double res = 0; + int counter = 0; + + switch (norm) + { + case 0: + res = INFINITY; + break; + case 1: /*Addition of all elements*/ + for (counter=0; counter < size; counter++) + { + sum += in[counter]; + } + res = sum; + break; + case 2: /*Square root of addition of squares of all elements*/ + for (counter=0; counter < size; counter++) + { + sum += in[counter]*in[counter]; + } + res = sqrt(sum); + break; + case 3: /*Cube root of addition of cubes of all elements*/ + for (counter=0; counter < size; counter++) + { + sum += pow(in[counter],3); + } + res = cbrt(sum); + break; + default : /*Nth root of addition of all elements raised to n*/ + for (counter=0; counter < size; counter++) + { + sum += pow(in[counter],norm); + } + res = pow(sum, 1./norm); + break; + } + + return res; +} \ No newline at end of file diff --git a/src/c/matrixOperations/norm/snorma.c b/src/c/matrixOperations/norm/snorma.c new file mode 100644 index 0000000..4bf2ccd --- /dev/null +++ b/src/c/matrixOperations/norm/snorma.c @@ -0,0 +1,72 @@ + /* Copyright (C) 2016 - IIT Bombay - FOSSEE + + This file must be used under the terms of the CeCILL. + This source file is licensed as described in the file COPYING, which + you should have received as part of this distribution. The terms + are also available at + http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt + Author: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + + /*This function calculates norms for matrices*/ + /*Acceptable norms are: 1, 2, 'inf', 'fro', */ + +#include "norm.h" +#include + +float dnorma (float *in, int row, int col, int norm) +{ + float res = 0, sum = 0; + int col_count, row_count; + + switch (norm) + { + case 1: /*largest column sum*/ + for(col_count = 0; col_count < col; col_count++) + { + sum = 0; + for(row_count = 0; row_count < row; row_count++) + { + sum += in[col_count*row+row_count]; + } + if (sum >= res) + res = sum; + } + break; + + case 2: /*Largest singular value of the matrix*/ + break; + + case 3: /*inf: largest row sum*/ + + for(row_count = 0; row_count < row; row_count++) + { + sum = 0; + for(col_count = 0; col_count < col; col_count++) + { + sum += in[col_count*row + row_count]; + } + if(sum >= res) + res = sum; + } + break; + + case 4: /*Frobenius norm*/ + + for(row_count = 0; row_count < row; row_count++) + { + for(col_count = 0; col_count < col; col_count++) + { + sum += in[col_count*row + row_count] * in[col_count*row + row_count]; + } + } + res = sqrt(sum); + + break; + } + + + return res; +} \ No newline at end of file diff --git a/src/c/matrixOperations/norm/snormv.c b/src/c/matrixOperations/norm/snormv.c new file mode 100644 index 0000000..03e9ed6 --- /dev/null +++ b/src/c/matrixOperations/norm/snormv.c @@ -0,0 +1,60 @@ + /* Copyright (C) 2016 - IIT Bombay - FOSSEE + + This file must be used under the terms of the CeCILL. + This source file is licensed as described in the file COPYING, which + you should have received as part of this distribution. The terms + are also available at + http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt + Author: Siddhesh Wani + Organization: FOSSEE, IIT Bombay + Email: toolbox@scilab.in + */ + + /*This function calculates norms for vectors (row or column)*/ + +#include "norm.h" +#include + +float dnormv (float *in, int size, int norm) +{ + float sum = 0; + float res = 0; + int counter = 0; + + switch (norm) + { + case 0: + res = INFINITY; + break; + case 1: /*Addition of all elements*/ + for (counter=0; counter < size; counter++) + { + sum += in[counter]; + } + res = sum; + break; + case 2: /*Square root of addition of squares of all elements*/ + for (counter=0; counter < size; counter++) + { + sum += in[counter]*in[counter]; + } + res = sqrt(sum); + break; + case 3: /*Cube root of addition of cubes of all elements*/ + for (counter=0; counter < size; counter++) + { + sum += pow(in[counter],3); + } + res = cbrt(sum); + break; + default : /*Nth root of addition of all elements raised to n*/ + for (counter=0; counter < size; counter++) + { + sum += pow(in[counter],norm); + } + res = pow(sum, 1./norm); + break; + } + + return res; +} \ No newline at end of file -- cgit