summaryrefslogtreecommitdiff
path: root/src/c/matrixOperations
diff options
context:
space:
mode:
Diffstat (limited to 'src/c/matrixOperations')
-rw-r--r--src/c/matrixOperations/includes/norm.h32
-rw-r--r--src/c/matrixOperations/interfaces/int_norm.h53
-rw-r--r--src/c/matrixOperations/norm/dnorma.c72
-rw-r--r--src/c/matrixOperations/norm/dnormv.c60
-rw-r--r--src/c/matrixOperations/norm/snorma.c72
-rw-r--r--src/c/matrixOperations/norm/snormv.c60
6 files changed, 349 insertions, 0 deletions
diff --git a/src/c/matrixOperations/includes/norm.h b/src/c/matrixOperations/includes/norm.h
new file mode 100644
index 00000000..6ac4c06a
--- /dev/null
+++ b/src/c/matrixOperations/includes/norm.h
@@ -0,0 +1,32 @@
+ /* 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
+ */
+#ifndef __NORM_H__
+#define __NORM_H__
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+double dnormv (double *in, int size, int norm);
+
+double dnorma (double *in, int row, int col, int norm);
+
+float dnormv (float *in, int size, int norm);
+
+float dnorma (float *in, int row, int col, int norm);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /*__NORM_H__*/
diff --git a/src/c/matrixOperations/interfaces/int_norm.h b/src/c/matrixOperations/interfaces/int_norm.h
new file mode 100644
index 00000000..a42a6754
--- /dev/null
+++ b/src/c/matrixOperations/interfaces/int_norm.h
@@ -0,0 +1,53 @@
+ /* 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
+ */
+
+#ifndef __INT_NORM_H__
+#define __INT_NORM_H__
+
+#include "statMax.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define d0normd0(in) in
+#define s0norms0(in) in
+
+#define d2normd0(in,size,out) ((size[0] == 1) || (size[1]==1))? \
+ dnormv(in, size[0]*size[1], 2): \
+ dnorma(in, size[0], size[1], 2)
+
+#define s2norms0(in,size,out) ((size[0] == 1) || (size[1]==1))? \
+ snormv(in, size[0]*size[1], 2): \
+ snorma(in, size[0], size[1], 2)
+
+#define d2d0normd0(in,size,norm) ((size[0] == 1) || (size[1]==1))? \
+ dnormv(in,size[0]*size[1],norm) : \
+ dnorma(in,size[0],size[1],norm)
+
+#define s2s0norms0(in,size,norm) ((size[0] == 1) || (size[1]==1))? \
+ snormv(in,size[0]*size[1],norm) : \
+ snorma(in,size[0],size[1],norm)
+
+#define d2g2normd0(in,size,norm,normsize) ((size[0] == 1) || (size[1]==1))? \
+ (norm[0]=='i') ? dmaxa(in,size[0]*size[1]) : dnormv(in,size[0]*size[1],2) : \
+ (norm[0]=='i') ? dnorma(in,size[0],size[1],3) : dnorma(in,size[0],size[1],4)
+
+#define s2g2norms0(in,size,norm,normsize) ((size[0] == 1) || (size[1]==1))? \
+ (norm[0]=='i') ? smaxa(in,size[0]*size[1]) : snormv(in,size[0]*size[1],2) : \
+ (norm[0]=='i') ? snorma(in,size[0],size[1],3) : snorma(in,size[0],size[1],4)
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /*__INT_NORM_H__*/
diff --git a/src/c/matrixOperations/norm/dnorma.c b/src/c/matrixOperations/norm/dnorma.c
new file mode 100644
index 00000000..c912f857
--- /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 <math.h>
+
+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 00000000..5d20d606
--- /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 <math.h>
+
+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 00000000..4bf2ccda
--- /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 <math.h>
+
+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 00000000..03e9ed66
--- /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 <math.h>
+
+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