/// /// This file is part of ILNumerics Community Edition. /// /// ILNumerics Community Edition - high performance computing for applications. /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net /// /// ILNumerics Community Edition is free software: you can redistribute it and/or modify /// it under the terms of the GNU General Public License version 3 as published by /// the Free Software Foundation. /// /// ILNumerics Community Edition is distributed in the hope that it will be useful, /// but WITHOUT ANY WARRANTY; without even the implied warranty of /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /// GNU General Public License for more details. /// /// You should have received a copy of the GNU General Public License /// along with ILNumerics Community Edition. See the file License.txt in the root /// of your distribution package. If not, see . /// /// In addition this software uses the following components and/or licenses: /// /// ================================================================================= /// The Open Toolkit Library License /// /// Copyright (c) 2006 - 2009 the Open Toolkit library. /// /// Permission is hereby granted, free of charge, to any person obtaining a copy /// of this software and associated documentation files (the "Software"), to deal /// in the Software without restriction, including without limitation the rights to /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of /// the Software, and to permit persons to whom the Software is furnished to do /// so, subject to the following conditions: /// /// The above copyright notice and this permission notice shall be included in all /// copies or substantial portions of the Software. /// /// ================================================================================= /// using System; using System.Collections.Generic; using System.Text; using ILNumerics; namespace ILNumerics { /// /// A helper class that can be used to generate various simple yet non-trivial test data sets /// public class ILSpecialData : ILMath { /// /// Generate sinc function in 2D, useful for plotting examples /// /// Number of rows /// Number of columns /// 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. /// Matrix with sinc data in 2 dimensions public static ILRetArray sinc (int rows, int cols, float periods) { using (ILScope.Enter()) { ILArray X = repmat(vec(-cols,2.0,cols-1).T,rows,1) / cols*pi*2*periods; ILArray Y = repmat(vec(-rows,2.0,rows-1),1,cols) / rows*pi*2*periods; ILArray ret = sqrt(X * X + Y * Y); ret[ret == 0.0] = MachineParameterDouble.eps; ret.a = sin(ret)/ret; return ret; } } /// /// Generate sinc function in 2D, useful for plotting examples /// /// Number of rows /// Number of columns /// Matrix with sinc data in 2 dimensions /// The function generates 4 zero crossings in each direction public static ILRetArray sinc(int rows, int cols) { return sinc(rows, cols, 1.0f); } /// /// Generate sinc function in 2D, single precision, useful for plotting examples /// /// Number of rows /// Number of columns /// Matrix with sinc data in 2 dimensions /// The function generates 4 zero crossings in each direction public static ILRetArray sincf(int rows, int cols) { return tosingle(sinc(rows, cols, 1.0f)); } /// /// Generate sinc function in 2D, useful for plotting examples /// /// Number of rows /// Number of columns /// 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. /// Matrix with sinc data in 2 dimensions public static ILRetArray sincf(int rows, int cols, float periods) { return tosingle(sinc(rows,cols,periods)); } /// /// Create specified periods of sine and cosine data /// /// Number of samples /// Number of (full) periods to be generated, must be > 0 /// Matrix with sine data in first column, cosine data in second column public static ILRetArray sincos1D(int numSamples, double periods) { using (ILScope.Enter()) { ILArray t = linspace(0.0, 2 * pi * periods, numSamples); return horzcat(sin(t).T, cos(t).T); } } /// /// Create specified periods of sine and cosine data, single precision /// /// Number of samples /// Number of (full) periods to be generated, must be > 0 /// Matrix with sine data in first column, cosine data in second column public static ILRetArray sincos1Df(int numSamples, double periods) { using (ILScope.Enter()) { ILArray t = linspace(0, 2 * pi * periods, numSamples); return horzcat(sin(t).T, cos(t).T); } } /// /// Create demo data for surface plots looking like a waterfall /// /// Number of rows /// Number of columns /// Matrix with data showing a waterfall terrain. public static ILRetArray waterfall(int rows, int cols) { using (ILScope.Enter()) { ILArray a = rand(rows, cols); ILArray bord = rand(1, cols) * 3 + (rows / 2); for (int c = 0; c < cols; c++) { int b = (int)(bord[c] - sin(c / cols * pi) * cols / 5); a[vec(0.0, b), c] = a[vec(0.0, b), c] + 2.0; } return a; } } /// /// Create demo data for surface plots looking like a waterfall /// /// Number of rows /// Number of columns /// Matrix with data showing a waterfall terrain. public static ILRetArray waterfallf(int rows, int cols) { using (ILScope.Enter()) { ILArray a = rand(rows, cols); ILArray bord = rand(1, cols) * 3 + (rows / 2); for (int c = 0; c < cols; c++) { int b = (int)(bord[c] - sin(c / cols * pi) * cols / 5); a[vec(0.0, b), c] = a[vec(0.0, b), c] + 2.0; } return tosingle(a); } } /// /// Create surface data of a sphere /// /// Number of facettes per angle /// [Output] X coords /// [Output] Y coords /// [Output] Z coords public static void sphere(int n, ILOutArray X, ILOutArray Y, ILOutArray Z) { using (ILScope.Enter()) { ILArray phi = repmat(linspace(-pi, pi, n).T, 1, n); ILArray rho = repmat(linspace(0, pi, n), n, 1); Y.a = sin(phi) * sin(rho); X.a = cos(phi) * sin(rho); Z.a = cos(rho); } } /// /// Create surface data for a M�bius strip /// /// Granularity (number of facettes) /// Width /// Radius /// [Output] X coords /// [Output] Y coords /// [Output] Z coords /// M�bius strip is a surfcae, crated by cutting a regular strip, twisting one end by 180 deg and glueing /// both ends together again. public static void moebius(int n, double w, double R, ILOutArray X, ILOutArray Y, ILOutArray Z) { using (ILScope.Enter()) { ILArray s = repmat(linspace(-w, w, n), n, 1); ILArray t = repmat(linspace(0, 2 * pi, n).T, 1, n); X.a = (R + s * cos(0.5 * t)) * cos(t); Y.a = (R + s * cos(0.5 * t)) * sin(t); Z.a = s * sin(0.5 * t); } } } }