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 System.Runtime.InteropServices;
|
---|
44 | using ILNumerics.Storage;
|
---|
45 | using ILNumerics.Misc;
|
---|
46 | using ILNumerics.Native;
|
---|
47 | using ILNumerics.Exceptions;
|
---|
48 |
|
---|
49 | namespace ILNumerics {
|
---|
50 |
|
---|
51 | public partial class ILMath {
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Regulary spaced vector
|
---|
55 | /// </summary>
|
---|
56 | /// <param name="start">Start value</param>
|
---|
57 | /// <param name="end">End value</param>
|
---|
58 | /// <returns>Row vector of size 1xN, where N is the number of elements
|
---|
59 | /// between start and end, all equally spaced with interval 1. The last element
|
---|
60 | /// of the returned vector will be less than or equal to end, if start <![CDATA[<]]> end.</returns>
|
---|
61 | /// <remarks>This is the same as vector (start,1,end).</remarks>
|
---|
62 | /// <exception cref="ILNumerics.Exceptions.ILArgumentException">If start <![CDATA[>]]> end</exception>
|
---|
63 | public static ILRetArray<double> vec(ILBaseArray start, ILBaseArray end) {
|
---|
64 | return vec<double>(start, 1, end);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /// <summary>
|
---|
68 | /// Regularly spaced vector, spacing 1
|
---|
69 | /// </summary>
|
---|
70 | /// <param name="start">Start value</param>
|
---|
71 | /// <param name="end">End value</param>
|
---|
72 | /// <returns>Row vector of size 1xN, where N is the number of elements
|
---|
73 | /// between start and end, all equally spaced with stepsize of 1. The last element
|
---|
74 | /// of the returned vector will be less than or equal to end, if start < end. </returns>
|
---|
75 | public static ILRetArray<T> vec<T>(ILBaseArray start, ILBaseArray end) {
|
---|
76 | return vec<T>(start, 1, end);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Regulary spaced column vector
|
---|
81 | /// </summary>
|
---|
82 | /// <param name="start">Start value</param>
|
---|
83 | /// <param name="step">Step size</param>
|
---|
84 | /// <param name="end">End value</param>
|
---|
85 | /// <returns>Column vector of length N, where N is the number of elements
|
---|
86 | /// between start and end, all equally spaced with stepsize of 'step'. The last element
|
---|
87 | /// of the returned vector will be less than or equal to end, if start < end. If start
|
---|
88 | /// > end, the elements in the vector will linearly decrease from
|
---|
89 | /// start to end. In this case, step must be negative.</returns>
|
---|
90 | /// <remarks><para>The shape of the vector created is controlled by the setting switch <see cref="ILNumerics.Settings.CreateRowVectorsByDefault"/>.
|
---|
91 | /// This setting defaults to 'false' which will cause the creation of a column vector. </para></remarks>
|
---|
92 | public static ILRetArray<T> vec<T>(ILBaseArray start, ILBaseArray step, ILBaseArray end) {
|
---|
93 | using (ILScope.Enter(start, step, end)) {
|
---|
94 | if (object.Equals(start, null) || !start.IsNumeric || !start.IsScalar) throw new ILArgumentException("start must be numeric scalar");
|
---|
95 | if (object.Equals(step, null) || !step.IsNumeric || !step.IsScalar) throw new ILArgumentException("step must be numeric scalar");
|
---|
96 | if (object.Equals(end, null) || !end.IsNumeric || !end.IsScalar) throw new ILArgumentException("end must be numeric scalar");
|
---|
97 | double dStart = todouble(start).GetValue(0);
|
---|
98 | double dStep = todouble(step).GetValue(0);
|
---|
99 | double dEnd = todouble(end).GetValue(0);
|
---|
100 |
|
---|
101 | if (dStep == 0)
|
---|
102 | throw new ILArgumentException("step must not be 0");
|
---|
103 | if (dStart > dEnd && dStep > 0)
|
---|
104 | throw new ILArgumentException("if start > end, step must be negativ");
|
---|
105 | if (dStart < dEnd && dStep < 0)
|
---|
106 | throw new ILArgumentException("if start < end, step must be positiv");
|
---|
107 | int nrElements = (int)Math.Floor((dEnd - dStart) / dStep) + 1;
|
---|
108 | T[] data = ILMemoryPool.Pool.New<T>(nrElements);
|
---|
109 | if (false) {
|
---|
110 | |
---|
111 |
|
---|
112 | } else if (data is double[]) {
|
---|
113 | unsafe {
|
---|
114 |
|
---|
115 | double val = ( double)dStart;
|
---|
116 | fixed ( double* pDataConst = (data as double[])) {
|
---|
117 |
|
---|
118 | double* pData = pDataConst;
|
---|
119 |
|
---|
120 | double* lastElement = pDataConst + nrElements;
|
---|
121 | while (pData < lastElement) {
|
---|
122 | *pData++ = val;
|
---|
123 | val = ( double)(val + dStep);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | |
---|
128 | #region HYCALPER AUTO GENERATED CODE
|
---|
129 | |
---|
130 |
|
---|
131 | } else if (data is byte[]) {
|
---|
132 | unsafe {
|
---|
133 |
|
---|
134 | byte val = ( byte)dStart;
|
---|
135 | fixed ( byte* pDataConst = (data as byte[])) {
|
---|
136 |
|
---|
137 | byte* pData = pDataConst;
|
---|
138 |
|
---|
139 | byte* lastElement = pDataConst + nrElements;
|
---|
140 | while (pData < lastElement) {
|
---|
141 | *pData++ = val;
|
---|
142 | val = ( byte)(val + dStep);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | } else if (data is Int64[]) {
|
---|
148 | unsafe {
|
---|
149 |
|
---|
150 | Int64 val = ( Int64)dStart;
|
---|
151 | fixed ( Int64* pDataConst = (data as Int64[])) {
|
---|
152 |
|
---|
153 | Int64* pData = pDataConst;
|
---|
154 |
|
---|
155 | Int64* lastElement = pDataConst + nrElements;
|
---|
156 | while (pData < lastElement) {
|
---|
157 | *pData++ = val;
|
---|
158 | val = ( Int64)(val + dStep);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | } else if (data is complex[]) {
|
---|
164 | unsafe {
|
---|
165 |
|
---|
166 | complex val = ( complex)dStart;
|
---|
167 | fixed ( complex* pDataConst = (data as complex[])) {
|
---|
168 |
|
---|
169 | complex* pData = pDataConst;
|
---|
170 |
|
---|
171 | complex* lastElement = pDataConst + nrElements;
|
---|
172 | while (pData < lastElement) {
|
---|
173 | *pData++ = val;
|
---|
174 | val = ( complex)(val + dStep);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | } else if (data is fcomplex[]) {
|
---|
180 | unsafe {
|
---|
181 |
|
---|
182 | fcomplex val = ( fcomplex)dStart;
|
---|
183 | fixed ( fcomplex* pDataConst = (data as fcomplex[])) {
|
---|
184 |
|
---|
185 | fcomplex* pData = pDataConst;
|
---|
186 |
|
---|
187 | fcomplex* lastElement = pDataConst + nrElements;
|
---|
188 | while (pData < lastElement) {
|
---|
189 | *pData++ = val;
|
---|
190 | val = ( fcomplex)(val + dStep);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | } else if (data is Int32[]) {
|
---|
196 | unsafe {
|
---|
197 |
|
---|
198 | Int32 val = ( Int32)dStart;
|
---|
199 | fixed ( Int32* pDataConst = (data as Int32[])) {
|
---|
200 |
|
---|
201 | Int32* pData = pDataConst;
|
---|
202 |
|
---|
203 | Int32* lastElement = pDataConst + nrElements;
|
---|
204 | while (pData < lastElement) {
|
---|
205 | *pData++ = val;
|
---|
206 | val = ( Int32)(val + dStep);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | } else if (data is float[]) {
|
---|
212 | unsafe {
|
---|
213 |
|
---|
214 | float val = ( float)dStart;
|
---|
215 | fixed ( float* pDataConst = (data as float[])) {
|
---|
216 |
|
---|
217 | float* pData = pDataConst;
|
---|
218 |
|
---|
219 | float* lastElement = pDataConst + nrElements;
|
---|
220 | while (pData < lastElement) {
|
---|
221 | *pData++ = val;
|
---|
222 | val = ( float)(val + dStep);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
228 | } else {
|
---|
229 | throw new ILArgumentException(String.Format("vec is not defined for arrays of inner type '{0}'", typeof(T).Name));
|
---|
230 | }
|
---|
231 | if (Settings.CreateRowVectorsByDefault) {
|
---|
232 | return new ILRetArray<T>(data, 1, nrElements);
|
---|
233 | } else {
|
---|
234 | return new ILRetArray<T>(data, nrElements, 1);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | }
|
---|
240 | }
|
---|