source: src/main/java/agents/anac/y2015/Phoenix/GP/MatrixOperations.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 6.3 KB
Line 
1package agents.anac.y2015.Phoenix.GP;/* This file is part of the jgpml Project.
2 * http://github.com/renzodenardi/jgpml
3 *
4 * Copyright (c) 2011 Renzo De Nardi and Hugo Gravato-Marques
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28import agents.Jama.Matrix;
29
30/**
31 * Some useful operations defined over Matrices
32 */
33public class MatrixOperations {
34
35
36 /**
37 * Computes the exponential of the input <code>Matrix</code>
38 * @param A input <code>Matrix</code>
39 * @return exp(A) result
40 */
41 public static Matrix exp(Matrix A){
42
43 Matrix out = new Matrix(A.getRowDimension(),A.getColumnDimension());
44 for(int i=0; i<A.getRowDimension(); i++)
45 for(int j=0; j<A.getColumnDimension(); j++)
46 out.set(i,j,Math.exp(A.get(i,j)));
47
48 return out;
49 }
50
51 /**
52 * Sums across the rows of the <code>Matrix</code> and return the result as a single column <code>MAtrix</code>
53 * @param A input <code>Matrix</code>
54 * @return result
55 */
56
57 public static Matrix sumRows(Matrix A){
58 Matrix sum = new Matrix(A.getRowDimension(),1);
59 for(int i=0; i<A.getColumnDimension(); i++)
60 sum.plusEquals(A.getMatrix(0,A.getRowDimension()-1,i,i));
61 return sum;
62 }
63
64
65 /**
66 * Adds a value to each elemnts of the <code>Matrix</code>
67 * @param A <code>Matrix</code>
68 * @param val value to be added
69 * @return result
70 */
71 public static Matrix addValue(Matrix A,double val){
72 for(int i=0; i<A.getRowDimension(); i++)
73 for(int j=0; j<A.getColumnDimension(); j++)
74 A.set(i,j,A.get(i,j)+val);
75
76 return A;
77 }
78
79 /**
80 * Computes the arcsin of the input <code>Matrix</code> (element by element)
81 * @param A input <code>Matrix</code>
82 * @return asin(A) result
83 */
84 public static Matrix asin(Matrix A){
85
86 Matrix out = new Matrix(A.getRowDimension(),A.getColumnDimension());
87 for(int i=0; i<A.getRowDimension(); i++)
88 for(int j=0; j<A.getColumnDimension(); j++)
89 out.set(i,j,Math.asin(A.get(i,j)));
90
91 return out;
92 }
93
94 /**
95 * Computes the square root of the input <code>Matrix</code> (element by element)
96 * @param A input <code>Matrix</code>
97 * @return sqrt(A) result
98 */
99 public static Matrix sqrt(Matrix A){
100
101 Matrix out = new Matrix(A.getRowDimension(),A.getColumnDimension());
102 for(int i=0; i<A.getRowDimension(); i++)
103 for(int j=0; j<A.getColumnDimension(); j++)
104 out.set(i,j,Math.sqrt(A.get(i,j)));
105
106 return out;
107 }
108
109 /**
110 * If the argument is a row or column <code>Matrix</code> it returns a new diagonal <code>Matrix</code>
111 * with the input as diagonal elements.
112 * If the argument is a <code>Matrix</code> it returns the diagonal elements as a single column <code>Matrix</code>
113 * Is a clone of the Matlab's function diag(A)
114 * @param A input <code>Matrix</code>
115 * @return diag(A) result
116 */
117 public static Matrix diag(Matrix A){
118 Matrix diag =null;
119 if(A.getColumnDimension()==1 || A.getRowDimension()==1){
120 if(A.getColumnDimension()==1) {
121 diag = new Matrix(A.getRowDimension(),A.getRowDimension());
122 for(int i=0; i<diag.getColumnDimension(); i++)
123 diag.set(i,i,A.get(i,0));
124 } else {
125 diag = new Matrix(A.getColumnDimension(),A.getColumnDimension());
126 for(int i=0; i<diag.getRowDimension(); i++)
127 diag.set(i,i,A.get(0,i));
128 }
129 } else {
130
131 diag = new Matrix(A.getRowDimension(),1);
132 for(int i=0; i<diag.getRowDimension(); i++)
133 diag.set(i,0,A.get(i,i));
134 }
135
136 return diag;
137 }
138
139
140 public static Matrix mean(Matrix A){
141
142 if(A.getRowDimension()==1) {
143 double m = 0;
144 for(int i=0; i<A.getColumnDimension(); i++) m+=A.get(0,i);
145
146 Matrix M = new Matrix(1,1);
147 M.set(0,0,m/A.getColumnDimension());
148 return M;
149 } else {
150 Matrix M = new Matrix(1,A.getColumnDimension());
151 for(int i=0; i<A.getColumnDimension(); i++){
152 double m=0;
153 for(int j=0; j<A.getRowDimension(); j++){
154 m+=A.get(j,i);
155 }
156 M.set(0,i,m/A.getRowDimension());
157 }
158 return M;
159 }
160 }
161
162 public static Matrix std(Matrix A){
163
164 if(A.getRowDimension()==1) {
165 double m = 0; double var=0;
166 for(int i=0; i<A.getColumnDimension(); i++){
167 m = (m*(i-1) + A.get(0,i))/i;
168 var = var*(i-1)/i + ((A.get(0,i)-m)*(A.get(0,i)-m))/(i-1);
169 }
170 Matrix M = new Matrix(1,1);
171 M.set(0,0,Math.sqrt(var));
172 return M;
173 } else {
174 Matrix M = new Matrix(1,A.getColumnDimension());
175 for(int i=0; i<A.getColumnDimension(); i++){
176 double m=0; double var=0;
177 for(int j=0; j<A.getRowDimension(); j++){
178 m = (m*(j-1) + A.get(j,i))/j;
179 var = var*(j-1)/j + ((A.get(j,i)-m)*(A.get(j,i)-m))/(j-1);
180 }
181 M.set(0,i,Math.sqrt(var));
182 }
183 return M;
184 }
185 }
186
187
188
189}
190
Note: See TracBrowser for help on using the repository browser.