1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | using System;
|
---|
41 | using System.Collections.Generic;
|
---|
42 | using System.Text;
|
---|
43 | using ILNumerics.Storage;
|
---|
44 | using ILNumerics.Misc;
|
---|
45 | using ILNumerics.Exceptions;
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | namespace ILNumerics {
|
---|
50 |
|
---|
51 | public partial class ILMath {
|
---|
52 |
|
---|
53 | |
---|
54 | /// <summary>
|
---|
55 | /// Vector or matrix norm
|
---|
56 | /// </summary>
|
---|
57 | /// <param name="A">Input matrix/ vector</param>
|
---|
58 | /// <param name="degree">[Optional] Degree of norm (default = 2). For vectors this must be one of:
|
---|
59 | /// <list type="bullet">
|
---|
60 | /// <item>arbitrary double value : returns sum(pow(abs(A),degree))^(1/degree)</item>
|
---|
61 | /// <item>System.double.PositiveInfinity: return Max(abs(A))</item>
|
---|
62 | /// <item>System.double.NegativeInfinity: return Min(abs(A))</item>
|
---|
63 | /// </list>
|
---|
64 | /// For matrices this must be one out of:
|
---|
65 | /// <list type="bullet">
|
---|
66 | /// <item>0: returns Frobenius norm: sqrt(sum(diag(multiply(A, A[1]))))</item>
|
---|
67 | /// <item>1: returns 1-norm, max(sum(abs(A)))</item>
|
---|
68 | /// <item>2: returns the largest singular value of A, max(svd(A))</item>
|
---|
69 | /// <item>PositiveInfinity: returns max(sum(abs(A), 2)), the largest value of the sums along the rows</item>
|
---|
70 | /// </list>
|
---|
71 | /// </param>
|
---|
72 | /// <returns>Array of same type as input array A</returns>
|
---|
73 | public static ILRetArray< double > norm(ILInArray< double > A, double degree = 2) {
|
---|
74 | using (ILScope.Enter(A)) {
|
---|
75 | if (Object.Equals(A, null) || !A.IsMatrix)
|
---|
76 | throw new ILArgumentSizeException("input array must be matrix or vector.");
|
---|
77 | if (A.IsEmpty)
|
---|
78 | return new ILRetArray< double>(ILSize.Scalar1_1);
|
---|
79 | else if (A.IsVector) {
|
---|
80 | if (degree == Double.PositiveInfinity) {
|
---|
81 | return max(abs(A));
|
---|
82 | } else if (degree == Double.NegativeInfinity) {
|
---|
83 | return min(abs(A));
|
---|
84 | } else {
|
---|
85 | if (degree == 0.0)
|
---|
86 | return array< double>( ( double)Double.PositiveInfinity , 1, 1);
|
---|
87 | return pow(sum(pow(abs(A), ( double)degree)), ( double)(1.0 / degree));
|
---|
88 | }
|
---|
89 | } else {
|
---|
90 | if (degree == 1.0) {
|
---|
91 | return max(sum(abs(A)));
|
---|
92 | } else if (degree == 2.0) {
|
---|
93 | return max(svd(A));
|
---|
94 | } else if (degree == Double.PositiveInfinity) {
|
---|
95 | return max(sum(abs(A), 2));
|
---|
96 | } else if (degree == 0.0) {
|
---|
97 |
|
---|
98 | return sqrt(sum(diag <double>(multiply(A, A[1]))));
|
---|
99 | } else {
|
---|
100 | throw new ILArgumentException("invalid argument 'degree' supplied. valid for matrices: 0,1,2,Double.PositiveInfinity");
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | |
---|
106 | #region HYCALPER AUTO GENERATED CODE
|
---|
107 | |
---|
108 | /// <summary>
|
---|
109 | /// Vector or matrix norm
|
---|
110 | /// </summary>
|
---|
111 | /// <param name="A">Input matrix/ vector</param>
|
---|
112 | /// <param name="degree">[Optional] Degree of norm (default = 2). For vectors this must be one of:
|
---|
113 | /// <list type="bullet">
|
---|
114 | /// <item>arbitrary double value : returns sum(pow(abs(A),degree))^(1/degree)</item>
|
---|
115 | /// <item>System.double.PositiveInfinity: return Max(abs(A))</item>
|
---|
116 | /// <item>System.double.NegativeInfinity: return Min(abs(A))</item>
|
---|
117 | /// </list>
|
---|
118 | /// For matrices this must be one out of:
|
---|
119 | /// <list type="bullet">
|
---|
120 | /// <item>0: returns Frobenius norm: sqrt(sum(diag(multiply(A, A[1]))))</item>
|
---|
121 | /// <item>1: returns 1-norm, max(sum(abs(A)))</item>
|
---|
122 | /// <item>2: returns the largest singular value of A, max(svd(A))</item>
|
---|
123 | /// <item>PositiveInfinity: returns max(sum(abs(A), 2)), the largest value of the sums along the rows</item>
|
---|
124 | /// </list>
|
---|
125 | /// </param>
|
---|
126 | /// <returns>Array of same type as input array A</returns>
|
---|
127 | public static ILRetArray< float > norm(ILInArray< fcomplex > A, double degree = 2) {
|
---|
128 | using (ILScope.Enter(A)) {
|
---|
129 | if (Object.Equals(A, null) || !A.IsMatrix)
|
---|
130 | throw new ILArgumentSizeException("input array must be matrix or vector.");
|
---|
131 | if (A.IsEmpty)
|
---|
132 | return new ILRetArray< float>(ILSize.Scalar1_1);
|
---|
133 | else if (A.IsVector) {
|
---|
134 | if (degree == Double.PositiveInfinity) {
|
---|
135 | return max(abs(A));
|
---|
136 | } else if (degree == Double.NegativeInfinity) {
|
---|
137 | return min(abs(A));
|
---|
138 | } else {
|
---|
139 | if (degree == 0.0)
|
---|
140 | return array< float>( ( float)Double.PositiveInfinity , 1, 1);
|
---|
141 | return pow(sum(pow(abs(A), ( float)degree)), ( float)(1.0 / degree));
|
---|
142 | }
|
---|
143 | } else {
|
---|
144 | if (degree == 1.0) {
|
---|
145 | return max(sum(abs(A)));
|
---|
146 | } else if (degree == 2.0) {
|
---|
147 | return max(svd(A));
|
---|
148 | } else if (degree == Double.PositiveInfinity) {
|
---|
149 | return max(sum(abs(A), 2));
|
---|
150 | } else if (degree == 0.0) {
|
---|
151 | return sqrt(sum(real(diag<fcomplex>(multiply(A, A[1])))));
|
---|
152 | } else {
|
---|
153 | throw new ILArgumentException("invalid argument 'degree' supplied. valid for matrices: 0,1,2,Double.PositiveInfinity");
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | /// <summary>
|
---|
159 | /// Vector or matrix norm
|
---|
160 | /// </summary>
|
---|
161 | /// <param name="A">Input matrix/ vector</param>
|
---|
162 | /// <param name="degree">[Optional] Degree of norm (default = 2). For vectors this must be one of:
|
---|
163 | /// <list type="bullet">
|
---|
164 | /// <item>arbitrary double value : returns sum(pow(abs(A),degree))^(1/degree)</item>
|
---|
165 | /// <item>System.double.PositiveInfinity: return Max(abs(A))</item>
|
---|
166 | /// <item>System.double.NegativeInfinity: return Min(abs(A))</item>
|
---|
167 | /// </list>
|
---|
168 | /// For matrices this must be one out of:
|
---|
169 | /// <list type="bullet">
|
---|
170 | /// <item>0: returns Frobenius norm: sqrt(sum(diag(multiply(A, A[1]))))</item>
|
---|
171 | /// <item>1: returns 1-norm, max(sum(abs(A)))</item>
|
---|
172 | /// <item>2: returns the largest singular value of A, max(svd(A))</item>
|
---|
173 | /// <item>PositiveInfinity: returns max(sum(abs(A), 2)), the largest value of the sums along the rows</item>
|
---|
174 | /// </list>
|
---|
175 | /// </param>
|
---|
176 | /// <returns>Array of same type as input array A</returns>
|
---|
177 | public static ILRetArray< float > norm(ILInArray< float > A, double degree = 2) {
|
---|
178 | using (ILScope.Enter(A)) {
|
---|
179 | if (Object.Equals(A, null) || !A.IsMatrix)
|
---|
180 | throw new ILArgumentSizeException("input array must be matrix or vector.");
|
---|
181 | if (A.IsEmpty)
|
---|
182 | return new ILRetArray< float>(ILSize.Scalar1_1);
|
---|
183 | else if (A.IsVector) {
|
---|
184 | if (degree == Double.PositiveInfinity) {
|
---|
185 | return max(abs(A));
|
---|
186 | } else if (degree == Double.NegativeInfinity) {
|
---|
187 | return min(abs(A));
|
---|
188 | } else {
|
---|
189 | if (degree == 0.0)
|
---|
190 | return array< float>( ( float)Double.PositiveInfinity , 1, 1);
|
---|
191 | return pow(sum(pow(abs(A), ( float)degree)), ( float)(1.0 / degree));
|
---|
192 | }
|
---|
193 | } else {
|
---|
194 | if (degree == 1.0) {
|
---|
195 | return max(sum(abs(A)));
|
---|
196 | } else if (degree == 2.0) {
|
---|
197 | return max(svd(A));
|
---|
198 | } else if (degree == Double.PositiveInfinity) {
|
---|
199 | return max(sum(abs(A), 2));
|
---|
200 | } else if (degree == 0.0) {
|
---|
201 | return sqrt(sum(diag<float>(multiply(A, A[1]))));
|
---|
202 | } else {
|
---|
203 | throw new ILArgumentException("invalid argument 'degree' supplied. valid for matrices: 0,1,2,Double.PositiveInfinity");
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | /// <summary>
|
---|
209 | /// Vector or matrix norm
|
---|
210 | /// </summary>
|
---|
211 | /// <param name="A">Input matrix/ vector</param>
|
---|
212 | /// <param name="degree">[Optional] Degree of norm (default = 2). For vectors this must be one of:
|
---|
213 | /// <list type="bullet">
|
---|
214 | /// <item>arbitrary double value : returns sum(pow(abs(A),degree))^(1/degree)</item>
|
---|
215 | /// <item>System.double.PositiveInfinity: return Max(abs(A))</item>
|
---|
216 | /// <item>System.double.NegativeInfinity: return Min(abs(A))</item>
|
---|
217 | /// </list>
|
---|
218 | /// For matrices this must be one out of:
|
---|
219 | /// <list type="bullet">
|
---|
220 | /// <item>0: returns Frobenius norm: sqrt(sum(diag(multiply(A, A[1]))))</item>
|
---|
221 | /// <item>1: returns 1-norm, max(sum(abs(A)))</item>
|
---|
222 | /// <item>2: returns the largest singular value of A, max(svd(A))</item>
|
---|
223 | /// <item>PositiveInfinity: returns max(sum(abs(A), 2)), the largest value of the sums along the rows</item>
|
---|
224 | /// </list>
|
---|
225 | /// </param>
|
---|
226 | /// <returns>Array of same type as input array A</returns>
|
---|
227 | public static ILRetArray< double > norm(ILInArray< complex > A, double degree = 2) {
|
---|
228 | using (ILScope.Enter(A)) {
|
---|
229 | if (Object.Equals(A, null) || !A.IsMatrix)
|
---|
230 | throw new ILArgumentSizeException("input array must be matrix or vector.");
|
---|
231 | if (A.IsEmpty)
|
---|
232 | return new ILRetArray< double>(ILSize.Scalar1_1);
|
---|
233 | else if (A.IsVector) {
|
---|
234 | if (degree == Double.PositiveInfinity) {
|
---|
235 | return max(abs(A));
|
---|
236 | } else if (degree == Double.NegativeInfinity) {
|
---|
237 | return min(abs(A));
|
---|
238 | } else {
|
---|
239 | if (degree == 0.0)
|
---|
240 | return array< double>( ( double)Double.PositiveInfinity , 1, 1);
|
---|
241 | return pow(sum(pow(abs(A), ( double)degree)), ( double)(1.0 / degree));
|
---|
242 | }
|
---|
243 | } else {
|
---|
244 | if (degree == 1.0) {
|
---|
245 | return max(sum(abs(A)));
|
---|
246 | } else if (degree == 2.0) {
|
---|
247 | return max(svd(A));
|
---|
248 | } else if (degree == Double.PositiveInfinity) {
|
---|
249 | return max(sum(abs(A), 2));
|
---|
250 | } else if (degree == 0.0) {
|
---|
251 | return sqrt(sum(real(diag<complex>(multiply(A, A[1])))));
|
---|
252 | } else {
|
---|
253 | throw new ILArgumentException("invalid argument 'degree' supplied. valid for matrices: 0,1,2,Double.PositiveInfinity");
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
260 |
|
---|
261 | }
|
---|
262 |
|
---|
263 | }
|
---|