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;
|
---|
44 |
|
---|
45 | namespace ILNumerics {
|
---|
46 | /// <summary>
|
---|
47 | /// A helper class that can be used to generate various simple yet non-trivial test data sets
|
---|
48 | /// </summary>
|
---|
49 | public class ILSpecialData : ILMath {
|
---|
50 | /// <summary>
|
---|
51 | /// Generate sinc function in 2D, useful for plotting examples
|
---|
52 | /// </summary>
|
---|
53 | /// <param name="rows">Number of rows</param>
|
---|
54 | /// <param name="cols">Number of columns</param>
|
---|
55 | /// <param name="periods">Influences the number of periods to be drawn in both directions. 1 will result in 4 zero crossings, higher values result in more, lower values in less zero crossings.</param>
|
---|
56 | /// <returns>Matrix with sinc data in 2 dimensions</returns>
|
---|
57 | public static ILRetArray<double> sinc (int rows, int cols, float periods) {
|
---|
58 | using (ILScope.Enter()) {
|
---|
59 | ILArray<double> X = repmat<double>(vec<double>(-cols,2.0,cols-1).T,rows,1) / cols*pi*2*periods;
|
---|
60 | ILArray<double> Y = repmat<double>(vec<double>(-rows,2.0,rows-1),1,cols) / rows*pi*2*periods;
|
---|
61 | ILArray<double> ret = sqrt(X * X + Y * Y);
|
---|
62 | ret[ret == 0.0] = MachineParameterDouble.eps;
|
---|
63 | ret.a = sin(ret)/ret;
|
---|
64 | return ret;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | /// <summary>
|
---|
68 | /// Generate sinc function in 2D, useful for plotting examples
|
---|
69 | /// </summary>
|
---|
70 | /// <param name="rows">Number of rows</param>
|
---|
71 | /// <param name="cols">Number of columns</param>
|
---|
72 | /// <returns>Matrix with sinc data in 2 dimensions</returns>
|
---|
73 | /// <remarks>The function generates 4 zero crossings in each direction</remarks>
|
---|
74 | public static ILRetArray<double> sinc(int rows, int cols) {
|
---|
75 | return sinc(rows, cols, 1.0f);
|
---|
76 | }
|
---|
77 | /// <summary>
|
---|
78 | /// Generate sinc function in 2D, single precision, useful for plotting examples
|
---|
79 | /// </summary>
|
---|
80 | /// <param name="rows">Number of rows</param>
|
---|
81 | /// <param name="cols">Number of columns</param>
|
---|
82 | /// <returns>Matrix with sinc data in 2 dimensions</returns>
|
---|
83 | /// <remarks>The function generates 4 zero crossings in each direction</remarks>
|
---|
84 | public static ILRetArray<float> sincf(int rows, int cols) {
|
---|
85 | return tosingle(sinc(rows, cols, 1.0f));
|
---|
86 | }
|
---|
87 | /// <summary>
|
---|
88 | /// Generate sinc function in 2D, useful for plotting examples
|
---|
89 | /// </summary>
|
---|
90 | /// <param name="rows">Number of rows</param>
|
---|
91 | /// <param name="cols">Number of columns</param>
|
---|
92 | /// <param name="periods">Influences the number of periods to be drawn in both directions. 1 will result in 4 zero crossings, higher values result in more, lower values in less zero crossings.</param>
|
---|
93 | /// <returns>Matrix with sinc data in 2 dimensions</returns>
|
---|
94 | public static ILRetArray<float> sincf(int rows, int cols, float periods) {
|
---|
95 | return tosingle(sinc(rows,cols,periods));
|
---|
96 | }
|
---|
97 |
|
---|
98 | /// <summary>
|
---|
99 | /// Create specified periods of sine and cosine data
|
---|
100 | /// </summary>
|
---|
101 | /// <param name="numSamples">Number of samples</param>
|
---|
102 | /// <param name="periods">Number of (full) periods to be generated, must be > 0</param>
|
---|
103 | /// <returns>Matrix with sine data in first column, cosine data in second column</returns>
|
---|
104 | public static ILRetArray<double> sincos1D(int numSamples, double periods) {
|
---|
105 | using (ILScope.Enter()) {
|
---|
106 | ILArray<double> t = linspace(0.0, 2 * pi * periods, numSamples);
|
---|
107 | return horzcat(sin(t).T, cos(t).T);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | /// <summary>
|
---|
111 | /// Create specified periods of sine and cosine data, single precision
|
---|
112 | /// </summary>
|
---|
113 | /// <param name="numSamples">Number of samples</param>
|
---|
114 | /// <param name="periods">Number of (full) periods to be generated, must be > 0</param>
|
---|
115 | /// <returns>Matrix with sine data in first column, cosine data in second column</returns>
|
---|
116 | public static ILRetArray<float> sincos1Df(int numSamples, double periods) {
|
---|
117 | using (ILScope.Enter()) {
|
---|
118 | ILArray<float> t = linspace<float>(0, 2 * pi * periods, numSamples);
|
---|
119 | return horzcat(sin(t).T, cos(t).T);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | /// <summary>
|
---|
123 | /// Create demo data for surface plots looking like a waterfall
|
---|
124 | /// </summary>
|
---|
125 | /// <param name="rows">Number of rows</param>
|
---|
126 | /// <param name="cols">Number of columns</param>
|
---|
127 | /// <returns>Matrix with data showing a waterfall terrain. </returns>
|
---|
128 | public static ILRetArray<double> waterfall(int rows, int cols) {
|
---|
129 | using (ILScope.Enter()) {
|
---|
130 | ILArray<double> a = rand(rows, cols);
|
---|
131 | ILArray<double> bord = rand(1, cols) * 3 + (rows / 2);
|
---|
132 | for (int c = 0; c < cols; c++) {
|
---|
133 | int b = (int)(bord[c] - sin(c / cols * pi) * cols / 5);
|
---|
134 | a[vec(0.0, b), c] = a[vec(0.0, b), c] + 2.0;
|
---|
135 | }
|
---|
136 | return a;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /// <summary>
|
---|
141 | /// Create demo data for surface plots looking like a waterfall
|
---|
142 | /// </summary>
|
---|
143 | /// <param name="rows">Number of rows</param>
|
---|
144 | /// <param name="cols">Number of columns</param>
|
---|
145 | /// <returns>Matrix with data showing a waterfall terrain. </returns>
|
---|
146 | public static ILRetArray<float> waterfallf(int rows, int cols) {
|
---|
147 | using (ILScope.Enter()) {
|
---|
148 | ILArray<double> a = rand(rows, cols);
|
---|
149 | ILArray<double> bord = rand(1, cols) * 3 + (rows / 2);
|
---|
150 | for (int c = 0; c < cols; c++) {
|
---|
151 | int b = (int)(bord[c] - sin(c / cols * pi) * cols / 5);
|
---|
152 | a[vec(0.0, b), c] = a[vec(0.0, b), c] + 2.0;
|
---|
153 | }
|
---|
154 | return tosingle(a);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /// <summary>
|
---|
159 | /// Create surface data of a sphere
|
---|
160 | /// </summary>
|
---|
161 | /// <param name="n">Number of facettes per angle</param>
|
---|
162 | /// <param name="X">[Output] X coords</param>
|
---|
163 | /// <param name="Y">[Output] Y coords</param>
|
---|
164 | /// <param name="Z">[Output] Z coords</param>
|
---|
165 | public static void sphere(int n, ILOutArray<double> X, ILOutArray<double> Y, ILOutArray<double> Z) {
|
---|
166 | using (ILScope.Enter()) {
|
---|
167 | ILArray<double> phi = repmat(linspace(-pi, pi, n).T, 1, n);
|
---|
168 | ILArray<double> rho = repmat(linspace(0, pi, n), n, 1);
|
---|
169 | Y.a = sin(phi) * sin(rho);
|
---|
170 | X.a = cos(phi) * sin(rho);
|
---|
171 | Z.a = cos(rho);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | /// <summary>
|
---|
175 | /// Create surface data for a Mï¿œbius strip
|
---|
176 | /// </summary>
|
---|
177 | /// <param name="n">Granularity (number of facettes)</param>
|
---|
178 | /// <param name="w">Width</param>
|
---|
179 | /// <param name="R">Radius</param>
|
---|
180 | /// <param name="X">[Output] X coords</param>
|
---|
181 | /// <param name="Y">[Output] Y coords</param>
|
---|
182 | /// <param name="Z">[Output] Z coords</param>
|
---|
183 | /// <remarks>Mï¿œbius strip is a surfcae, crated by cutting a regular strip, twisting one end by 180 deg and glueing
|
---|
184 | /// both ends together again.</remarks>
|
---|
185 | public static void moebius(int n, double w, double R, ILOutArray<double> X, ILOutArray<double> Y, ILOutArray<double> Z) {
|
---|
186 | using (ILScope.Enter()) {
|
---|
187 | ILArray<double> s = repmat(linspace(-w, w, n), n, 1);
|
---|
188 | ILArray<double> t = repmat(linspace(0, 2 * pi, n).T, 1, n);
|
---|
189 | X.a = (R + s * cos(0.5 * t)) * cos(t);
|
---|
190 | Y.a = (R + s * cos(0.5 * t)) * sin(t);
|
---|
191 | Z.a = s * sin(0.5 * t);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | }
|
---|
197 | }
|
---|