[9102] | 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 | public partial class ILMath {
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | |
---|
| 54 | /// <summary>
|
---|
| 55 | /// Sum elements of A along dimension specified.
|
---|
| 56 | /// </summary>
|
---|
| 57 | /// <param name="A">Input array</param>
|
---|
| 58 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
| 59 | /// <returns>Array, same size as A, but having the specified or first non singleton dimension reduced to the length 1 with the sum of all
|
---|
| 60 | /// elements along that dimension.</returns>
|
---|
| 61 | public static ILRetArray<double> sum (ILInArray<double> A, int dim = -1) {
|
---|
| 62 | using (ILScope.Enter(A)) {
|
---|
| 63 | if (dim < 0)
|
---|
| 64 | dim = A.Size.WorkingDimension();
|
---|
| 65 | if (dim >= A.Size.NumberOfDimensions)
|
---|
| 66 | throw new ILArgumentException("dimension parameter out of range!");
|
---|
| 67 | if (A.IsEmpty)
|
---|
| 68 | return new ILRetArray<double>(A.Size);
|
---|
| 69 | if (A.IsScalar) {
|
---|
| 70 |
|
---|
| 71 | return new ILRetArray<double>(new double[] { A.GetValue(0) }, 1, 1);
|
---|
| 72 | }
|
---|
| 73 | ILSize inDim = A.Size;
|
---|
| 74 | int[] newDims = inDim.ToIntArray();
|
---|
| 75 |
|
---|
| 76 | if (inDim[dim] == 1) return A.C;
|
---|
| 77 |
|
---|
| 78 | int newLength;
|
---|
| 79 |
|
---|
| 80 | double[] retArr;
|
---|
| 81 | // build ILSize
|
---|
| 82 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
| 83 | newDims[dim] = 1;
|
---|
| 84 | retArr = ILMemoryPool.Pool.New< double>(newLength);
|
---|
| 85 | ILSize newDimension = new ILSize(newDims);
|
---|
| 86 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
| 87 | int dimLen = inDim[dim];
|
---|
| 88 | int nrHigherDims = inDim.NumberOfElements / dimLen;
|
---|
| 89 | if (dim == 0) {
|
---|
| 90 | #region physical along 1st leading dimension
|
---|
| 91 | unsafe {
|
---|
| 92 | fixed ( double* pOutArr = retArr)
|
---|
| 93 | fixed ( double* pInArr = A.GetArrayForRead()) {
|
---|
| 94 |
|
---|
| 95 | double* lastElement;
|
---|
| 96 |
|
---|
| 97 | double* tmpOut = pOutArr;
|
---|
| 98 |
|
---|
| 99 | double* tmpIn = pInArr;
|
---|
| 100 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 101 | lastElement = tmpIn + dimLen;
|
---|
| 102 |
|
---|
| 103 | *tmpOut = 0.0;
|
---|
| 104 | while (tmpIn < lastElement) {
|
---|
| 105 |
|
---|
| 106 | double inVal = *(tmpIn++);
|
---|
| 107 |
|
---|
| 108 | /*dummy*/
|
---|
| 109 |
|
---|
| 110 | *tmpOut += (double) /*dummy*/ (inVal) ;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /*dummy*/
|
---|
| 114 | tmpOut++;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | #endregion
|
---|
| 119 | } else {
|
---|
| 120 | #region physical along abitrary dimension
|
---|
| 121 | // sum along abitrary dimension
|
---|
| 122 | unsafe {
|
---|
| 123 | fixed ( double* pOutArr = retArr)
|
---|
| 124 | fixed ( double* pInArr = A.GetArrayForRead()) {
|
---|
| 125 |
|
---|
| 126 | double* lastElementOut = newLength + pOutArr - 1;
|
---|
| 127 | int inLength = inDim.NumberOfElements - 1;
|
---|
| 128 |
|
---|
| 129 | double* lastElementIn = pInArr + inLength;
|
---|
| 130 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
| 131 |
|
---|
| 132 | double* tmpOut = pOutArr;
|
---|
| 133 | int outLength = newLength - 1;
|
---|
| 134 |
|
---|
| 135 | double* leadEnd;
|
---|
| 136 |
|
---|
| 137 | double* tmpIn = pInArr;
|
---|
| 138 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 139 | leadEnd = tmpIn + dimLen * inc;
|
---|
| 140 |
|
---|
| 141 | *tmpOut = 0.0;
|
---|
| 142 | while (tmpIn < leadEnd) {
|
---|
| 143 |
|
---|
| 144 | double inVal = *(tmpIn);
|
---|
| 145 | tmpIn += inc;
|
---|
| 146 |
|
---|
| 147 | /*dummy*/
|
---|
| 148 |
|
---|
| 149 | *tmpOut += (double) /*dummy*/ (inVal) ; //
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /*dummy*/
|
---|
| 153 | tmpOut += inc;
|
---|
| 154 | if (tmpOut > lastElementOut)
|
---|
| 155 | tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength);
|
---|
| 156 | if (tmpIn > lastElementIn)
|
---|
| 157 | tmpIn = pInArr + ((tmpIn - pInArr) - inLength);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | #endregion
|
---|
| 162 | }
|
---|
| 163 | return new ILRetArray<double>(retArr, newDims);
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | |
---|
| 167 | #region HYCALPER AUTO GENERATED CODE
|
---|
| 168 | |
---|
| 169 | /// <summary>
|
---|
| 170 | /// Sum elements of A along dimension specified.
|
---|
| 171 | /// </summary>
|
---|
| 172 | /// <param name="A">Input array</param>
|
---|
| 173 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
| 174 | /// <returns>Array, same size as A, but having the specified or first non singleton dimension reduced to the length 1 with the sum of all
|
---|
| 175 | /// elements along that dimension.</returns>
|
---|
| 176 | public static ILRetArray<Int64> sum (ILInArray<Int64> A, int dim = -1) {
|
---|
| 177 | using (ILScope.Enter(A)) {
|
---|
| 178 | if (dim < 0)
|
---|
| 179 | dim = A.Size.WorkingDimension();
|
---|
| 180 | if (dim >= A.Size.NumberOfDimensions)
|
---|
| 181 | throw new ILArgumentException("dimension parameter out of range!");
|
---|
| 182 | if (A.IsEmpty)
|
---|
| 183 | return new ILRetArray<Int64>(A.Size);
|
---|
| 184 | if (A.IsScalar) {
|
---|
| 185 |
|
---|
| 186 | return new ILRetArray<Int64>(new Int64[] { A.GetValue(0) }, 1, 1);
|
---|
| 187 | }
|
---|
| 188 | ILSize inDim = A.Size;
|
---|
| 189 | int[] newDims = inDim.ToIntArray();
|
---|
| 190 |
|
---|
| 191 | if (inDim[dim] == 1) return A.C;
|
---|
| 192 |
|
---|
| 193 | int newLength;
|
---|
| 194 |
|
---|
| 195 | Int64[] retArr;
|
---|
| 196 | // build ILSize
|
---|
| 197 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
| 198 | newDims[dim] = 1;
|
---|
| 199 | retArr = ILMemoryPool.Pool.New< Int64>(newLength);
|
---|
| 200 | ILSize newDimension = new ILSize(newDims);
|
---|
| 201 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
| 202 | int dimLen = inDim[dim];
|
---|
| 203 | int nrHigherDims = inDim.NumberOfElements / dimLen;
|
---|
| 204 | if (dim == 0) {
|
---|
| 205 | #region physical along 1st leading dimension
|
---|
| 206 | unsafe {
|
---|
| 207 | fixed ( Int64* pOutArr = retArr)
|
---|
| 208 | fixed ( Int64* pInArr = A.GetArrayForRead()) {
|
---|
| 209 |
|
---|
| 210 | Int64* lastElement;
|
---|
| 211 |
|
---|
| 212 | Int64* tmpOut = pOutArr;
|
---|
| 213 |
|
---|
| 214 | Int64* tmpIn = pInArr;
|
---|
| 215 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 216 | lastElement = tmpIn + dimLen;
|
---|
| 217 | *tmpOut = 0;
|
---|
| 218 | while (tmpIn < lastElement) {
|
---|
| 219 |
|
---|
| 220 | Int64 inVal = *(tmpIn++);
|
---|
| 221 |
|
---|
| 222 | /*dummy*/
|
---|
| 223 |
|
---|
| 224 | *tmpOut += (Int64) /*dummy*/ (inVal) ;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /*dummy*/
|
---|
| 228 | tmpOut++;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | #endregion
|
---|
| 233 | } else {
|
---|
| 234 | #region physical along abitrary dimension
|
---|
| 235 | // sum along abitrary dimension
|
---|
| 236 | unsafe {
|
---|
| 237 | fixed ( Int64* pOutArr = retArr)
|
---|
| 238 | fixed ( Int64* pInArr = A.GetArrayForRead()) {
|
---|
| 239 |
|
---|
| 240 | Int64* lastElementOut = newLength + pOutArr - 1;
|
---|
| 241 | int inLength = inDim.NumberOfElements - 1;
|
---|
| 242 |
|
---|
| 243 | Int64* lastElementIn = pInArr + inLength;
|
---|
| 244 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
| 245 |
|
---|
| 246 | Int64* tmpOut = pOutArr;
|
---|
| 247 | int outLength = newLength - 1;
|
---|
| 248 |
|
---|
| 249 | Int64* leadEnd;
|
---|
| 250 |
|
---|
| 251 | Int64* tmpIn = pInArr;
|
---|
| 252 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 253 | leadEnd = tmpIn + dimLen * inc;
|
---|
| 254 | *tmpOut = 0;
|
---|
| 255 | while (tmpIn < leadEnd) {
|
---|
| 256 |
|
---|
| 257 | Int64 inVal = *(tmpIn);
|
---|
| 258 | tmpIn += inc;
|
---|
| 259 |
|
---|
| 260 | /*dummy*/
|
---|
| 261 |
|
---|
| 262 | *tmpOut += (Int64) /*dummy*/ (inVal) ; //
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /*dummy*/
|
---|
| 266 | tmpOut += inc;
|
---|
| 267 | if (tmpOut > lastElementOut)
|
---|
| 268 | tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength);
|
---|
| 269 | if (tmpIn > lastElementIn)
|
---|
| 270 | tmpIn = pInArr + ((tmpIn - pInArr) - inLength);
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | #endregion
|
---|
| 275 | }
|
---|
| 276 | return new ILRetArray<Int64>(retArr, newDims);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | /// <summary>
|
---|
| 280 | /// Sum elements of A along dimension specified.
|
---|
| 281 | /// </summary>
|
---|
| 282 | /// <param name="A">Input array</param>
|
---|
| 283 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
| 284 | /// <returns>Array, same size as A, but having the specified or first non singleton dimension reduced to the length 1 with the sum of all
|
---|
| 285 | /// elements along that dimension.</returns>
|
---|
| 286 | public static ILRetArray<Int32> sum (ILInArray<Int32> A, int dim = -1) {
|
---|
| 287 | using (ILScope.Enter(A)) {
|
---|
| 288 | if (dim < 0)
|
---|
| 289 | dim = A.Size.WorkingDimension();
|
---|
| 290 | if (dim >= A.Size.NumberOfDimensions)
|
---|
| 291 | throw new ILArgumentException("dimension parameter out of range!");
|
---|
| 292 | if (A.IsEmpty)
|
---|
| 293 | return new ILRetArray<Int32>(A.Size);
|
---|
| 294 | if (A.IsScalar) {
|
---|
| 295 |
|
---|
| 296 | return new ILRetArray<Int32>(new Int32[] { A.GetValue(0) }, 1, 1);
|
---|
| 297 | }
|
---|
| 298 | ILSize inDim = A.Size;
|
---|
| 299 | int[] newDims = inDim.ToIntArray();
|
---|
| 300 |
|
---|
| 301 | if (inDim[dim] == 1) return A.C;
|
---|
| 302 |
|
---|
| 303 | int newLength;
|
---|
| 304 |
|
---|
| 305 | Int32[] retArr;
|
---|
| 306 | // build ILSize
|
---|
| 307 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
| 308 | newDims[dim] = 1;
|
---|
| 309 | retArr = ILMemoryPool.Pool.New< Int32>(newLength);
|
---|
| 310 | ILSize newDimension = new ILSize(newDims);
|
---|
| 311 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
| 312 | int dimLen = inDim[dim];
|
---|
| 313 | int nrHigherDims = inDim.NumberOfElements / dimLen;
|
---|
| 314 | if (dim == 0) {
|
---|
| 315 | #region physical along 1st leading dimension
|
---|
| 316 | unsafe {
|
---|
| 317 | fixed ( Int32* pOutArr = retArr)
|
---|
| 318 | fixed ( Int32* pInArr = A.GetArrayForRead()) {
|
---|
| 319 |
|
---|
| 320 | Int32* lastElement;
|
---|
| 321 |
|
---|
| 322 | Int32* tmpOut = pOutArr;
|
---|
| 323 |
|
---|
| 324 | Int32* tmpIn = pInArr;
|
---|
| 325 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 326 | lastElement = tmpIn + dimLen;
|
---|
| 327 | *tmpOut = 0;
|
---|
| 328 | while (tmpIn < lastElement) {
|
---|
| 329 |
|
---|
| 330 | Int32 inVal = *(tmpIn++);
|
---|
| 331 |
|
---|
| 332 | /*dummy*/
|
---|
| 333 |
|
---|
| 334 | *tmpOut += (Int32) /*dummy*/ (inVal) ;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /*dummy*/
|
---|
| 338 | tmpOut++;
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 | #endregion
|
---|
| 343 | } else {
|
---|
| 344 | #region physical along abitrary dimension
|
---|
| 345 | // sum along abitrary dimension
|
---|
| 346 | unsafe {
|
---|
| 347 | fixed ( Int32* pOutArr = retArr)
|
---|
| 348 | fixed ( Int32* pInArr = A.GetArrayForRead()) {
|
---|
| 349 |
|
---|
| 350 | Int32* lastElementOut = newLength + pOutArr - 1;
|
---|
| 351 | int inLength = inDim.NumberOfElements - 1;
|
---|
| 352 |
|
---|
| 353 | Int32* lastElementIn = pInArr + inLength;
|
---|
| 354 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
| 355 |
|
---|
| 356 | Int32* tmpOut = pOutArr;
|
---|
| 357 | int outLength = newLength - 1;
|
---|
| 358 |
|
---|
| 359 | Int32* leadEnd;
|
---|
| 360 |
|
---|
| 361 | Int32* tmpIn = pInArr;
|
---|
| 362 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 363 | leadEnd = tmpIn + dimLen * inc;
|
---|
| 364 | *tmpOut = 0;
|
---|
| 365 | while (tmpIn < leadEnd) {
|
---|
| 366 |
|
---|
| 367 | Int32 inVal = *(tmpIn);
|
---|
| 368 | tmpIn += inc;
|
---|
| 369 |
|
---|
| 370 | /*dummy*/
|
---|
| 371 |
|
---|
| 372 | *tmpOut += (Int32) /*dummy*/ (inVal) ; //
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | /*dummy*/
|
---|
| 376 | tmpOut += inc;
|
---|
| 377 | if (tmpOut > lastElementOut)
|
---|
| 378 | tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength);
|
---|
| 379 | if (tmpIn > lastElementIn)
|
---|
| 380 | tmpIn = pInArr + ((tmpIn - pInArr) - inLength);
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 | #endregion
|
---|
| 385 | }
|
---|
| 386 | return new ILRetArray<Int32>(retArr, newDims);
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 | /// <summary>
|
---|
| 390 | /// Sum elements of A along dimension specified.
|
---|
| 391 | /// </summary>
|
---|
| 392 | /// <param name="A">Input array</param>
|
---|
| 393 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
| 394 | /// <returns>Array, same size as A, but having the specified or first non singleton dimension reduced to the length 1 with the sum of all
|
---|
| 395 | /// elements along that dimension.</returns>
|
---|
| 396 | public static ILRetArray<byte> sum (ILInArray<byte> A, int dim = -1) {
|
---|
| 397 | using (ILScope.Enter(A)) {
|
---|
| 398 | if (dim < 0)
|
---|
| 399 | dim = A.Size.WorkingDimension();
|
---|
| 400 | if (dim >= A.Size.NumberOfDimensions)
|
---|
| 401 | throw new ILArgumentException("dimension parameter out of range!");
|
---|
| 402 | if (A.IsEmpty)
|
---|
| 403 | return new ILRetArray<byte>(A.Size);
|
---|
| 404 | if (A.IsScalar) {
|
---|
| 405 |
|
---|
| 406 | return new ILRetArray<byte>(new byte[] { A.GetValue(0) }, 1, 1);
|
---|
| 407 | }
|
---|
| 408 | ILSize inDim = A.Size;
|
---|
| 409 | int[] newDims = inDim.ToIntArray();
|
---|
| 410 |
|
---|
| 411 | if (inDim[dim] == 1) return A.C;
|
---|
| 412 |
|
---|
| 413 | int newLength;
|
---|
| 414 |
|
---|
| 415 | byte[] retArr;
|
---|
| 416 | // build ILSize
|
---|
| 417 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
| 418 | newDims[dim] = 1;
|
---|
| 419 | retArr = ILMemoryPool.Pool.New< byte>(newLength);
|
---|
| 420 | ILSize newDimension = new ILSize(newDims);
|
---|
| 421 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
| 422 | int dimLen = inDim[dim];
|
---|
| 423 | int nrHigherDims = inDim.NumberOfElements / dimLen;
|
---|
| 424 | if (dim == 0) {
|
---|
| 425 | #region physical along 1st leading dimension
|
---|
| 426 | unsafe {
|
---|
| 427 | fixed ( byte* pOutArr = retArr)
|
---|
| 428 | fixed ( byte* pInArr = A.GetArrayForRead()) {
|
---|
| 429 |
|
---|
| 430 | byte* lastElement;
|
---|
| 431 |
|
---|
| 432 | byte* tmpOut = pOutArr;
|
---|
| 433 |
|
---|
| 434 | byte* tmpIn = pInArr;
|
---|
| 435 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 436 | lastElement = tmpIn + dimLen;
|
---|
| 437 | *tmpOut = 0;
|
---|
| 438 | while (tmpIn < lastElement) {
|
---|
| 439 |
|
---|
| 440 | byte inVal = *(tmpIn++);
|
---|
| 441 |
|
---|
| 442 | /*dummy*/
|
---|
| 443 |
|
---|
| 444 | *tmpOut += (byte) /*dummy*/ (inVal) ;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | /*dummy*/
|
---|
| 448 | tmpOut++;
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 | #endregion
|
---|
| 453 | } else {
|
---|
| 454 | #region physical along abitrary dimension
|
---|
| 455 | // sum along abitrary dimension
|
---|
| 456 | unsafe {
|
---|
| 457 | fixed ( byte* pOutArr = retArr)
|
---|
| 458 | fixed ( byte* pInArr = A.GetArrayForRead()) {
|
---|
| 459 |
|
---|
| 460 | byte* lastElementOut = newLength + pOutArr - 1;
|
---|
| 461 | int inLength = inDim.NumberOfElements - 1;
|
---|
| 462 |
|
---|
| 463 | byte* lastElementIn = pInArr + inLength;
|
---|
| 464 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
| 465 |
|
---|
| 466 | byte* tmpOut = pOutArr;
|
---|
| 467 | int outLength = newLength - 1;
|
---|
| 468 |
|
---|
| 469 | byte* leadEnd;
|
---|
| 470 |
|
---|
| 471 | byte* tmpIn = pInArr;
|
---|
| 472 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 473 | leadEnd = tmpIn + dimLen * inc;
|
---|
| 474 | *tmpOut = 0;
|
---|
| 475 | while (tmpIn < leadEnd) {
|
---|
| 476 |
|
---|
| 477 | byte inVal = *(tmpIn);
|
---|
| 478 | tmpIn += inc;
|
---|
| 479 |
|
---|
| 480 | /*dummy*/
|
---|
| 481 |
|
---|
| 482 | *tmpOut += (byte) /*dummy*/ (inVal) ; //
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | /*dummy*/
|
---|
| 486 | tmpOut += inc;
|
---|
| 487 | if (tmpOut > lastElementOut)
|
---|
| 488 | tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength);
|
---|
| 489 | if (tmpIn > lastElementIn)
|
---|
| 490 | tmpIn = pInArr + ((tmpIn - pInArr) - inLength);
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 | #endregion
|
---|
| 495 | }
|
---|
| 496 | return new ILRetArray<byte>(retArr, newDims);
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
| 499 | /// <summary>
|
---|
| 500 | /// Sum elements of A along dimension specified.
|
---|
| 501 | /// </summary>
|
---|
| 502 | /// <param name="A">Input array</param>
|
---|
| 503 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
| 504 | /// <returns>Array, same size as A, but having the specified or first non singleton dimension reduced to the length 1 with the sum of all
|
---|
| 505 | /// elements along that dimension.</returns>
|
---|
| 506 | public static ILRetArray<fcomplex> sum (ILInArray<fcomplex> A, int dim = -1) {
|
---|
| 507 | using (ILScope.Enter(A)) {
|
---|
| 508 | if (dim < 0)
|
---|
| 509 | dim = A.Size.WorkingDimension();
|
---|
| 510 | if (dim >= A.Size.NumberOfDimensions)
|
---|
| 511 | throw new ILArgumentException("dimension parameter out of range!");
|
---|
| 512 | if (A.IsEmpty)
|
---|
| 513 | return new ILRetArray<fcomplex>(A.Size);
|
---|
| 514 | if (A.IsScalar) {
|
---|
| 515 |
|
---|
| 516 | return new ILRetArray<fcomplex>(new fcomplex[] { A.GetValue(0) }, 1, 1);
|
---|
| 517 | }
|
---|
| 518 | ILSize inDim = A.Size;
|
---|
| 519 | int[] newDims = inDim.ToIntArray();
|
---|
| 520 |
|
---|
| 521 | if (inDim[dim] == 1) return A.C;
|
---|
| 522 |
|
---|
| 523 | int newLength;
|
---|
| 524 |
|
---|
| 525 | fcomplex[] retArr;
|
---|
| 526 | // build ILSize
|
---|
| 527 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
| 528 | newDims[dim] = 1;
|
---|
| 529 | retArr = ILMemoryPool.Pool.New< fcomplex>(newLength);
|
---|
| 530 | ILSize newDimension = new ILSize(newDims);
|
---|
| 531 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
| 532 | int dimLen = inDim[dim];
|
---|
| 533 | int nrHigherDims = inDim.NumberOfElements / dimLen;
|
---|
| 534 | if (dim == 0) {
|
---|
| 535 | #region physical along 1st leading dimension
|
---|
| 536 | unsafe {
|
---|
| 537 | fixed ( fcomplex* pOutArr = retArr)
|
---|
| 538 | fixed ( fcomplex* pInArr = A.GetArrayForRead()) {
|
---|
| 539 |
|
---|
| 540 | fcomplex* lastElement;
|
---|
| 541 |
|
---|
| 542 | fcomplex* tmpOut = pOutArr;
|
---|
| 543 |
|
---|
| 544 | fcomplex* tmpIn = pInArr;
|
---|
| 545 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 546 | lastElement = tmpIn + dimLen;
|
---|
| 547 | *tmpOut = 0.0f;
|
---|
| 548 | while (tmpIn < lastElement) {
|
---|
| 549 |
|
---|
| 550 | fcomplex inVal = *(tmpIn++);
|
---|
| 551 |
|
---|
| 552 | /*dummy*/
|
---|
| 553 |
|
---|
| 554 | *tmpOut += (fcomplex) /*dummy*/ (inVal) ;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | /*dummy*/
|
---|
| 558 | tmpOut++;
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
| 562 | #endregion
|
---|
| 563 | } else {
|
---|
| 564 | #region physical along abitrary dimension
|
---|
| 565 | // sum along abitrary dimension
|
---|
| 566 | unsafe {
|
---|
| 567 | fixed ( fcomplex* pOutArr = retArr)
|
---|
| 568 | fixed ( fcomplex* pInArr = A.GetArrayForRead()) {
|
---|
| 569 |
|
---|
| 570 | fcomplex* lastElementOut = newLength + pOutArr - 1;
|
---|
| 571 | int inLength = inDim.NumberOfElements - 1;
|
---|
| 572 |
|
---|
| 573 | fcomplex* lastElementIn = pInArr + inLength;
|
---|
| 574 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
| 575 |
|
---|
| 576 | fcomplex* tmpOut = pOutArr;
|
---|
| 577 | int outLength = newLength - 1;
|
---|
| 578 |
|
---|
| 579 | fcomplex* leadEnd;
|
---|
| 580 |
|
---|
| 581 | fcomplex* tmpIn = pInArr;
|
---|
| 582 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 583 | leadEnd = tmpIn + dimLen * inc;
|
---|
| 584 | *tmpOut = 0.0f;
|
---|
| 585 | while (tmpIn < leadEnd) {
|
---|
| 586 |
|
---|
| 587 | fcomplex inVal = *(tmpIn);
|
---|
| 588 | tmpIn += inc;
|
---|
| 589 |
|
---|
| 590 | /*dummy*/
|
---|
| 591 |
|
---|
| 592 | *tmpOut += (fcomplex) /*dummy*/ (inVal) ; //
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | /*dummy*/
|
---|
| 596 | tmpOut += inc;
|
---|
| 597 | if (tmpOut > lastElementOut)
|
---|
| 598 | tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength);
|
---|
| 599 | if (tmpIn > lastElementIn)
|
---|
| 600 | tmpIn = pInArr + ((tmpIn - pInArr) - inLength);
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 | }
|
---|
| 604 | #endregion
|
---|
| 605 | }
|
---|
| 606 | return new ILRetArray<fcomplex>(retArr, newDims);
|
---|
| 607 | }
|
---|
| 608 | }
|
---|
| 609 | /// <summary>
|
---|
| 610 | /// Sum elements of A along dimension specified.
|
---|
| 611 | /// </summary>
|
---|
| 612 | /// <param name="A">Input array</param>
|
---|
| 613 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
| 614 | /// <returns>Array, same size as A, but having the specified or first non singleton dimension reduced to the length 1 with the sum of all
|
---|
| 615 | /// elements along that dimension.</returns>
|
---|
| 616 | public static ILRetArray<float> sum (ILInArray<float> A, int dim = -1) {
|
---|
| 617 | using (ILScope.Enter(A)) {
|
---|
| 618 | if (dim < 0)
|
---|
| 619 | dim = A.Size.WorkingDimension();
|
---|
| 620 | if (dim >= A.Size.NumberOfDimensions)
|
---|
| 621 | throw new ILArgumentException("dimension parameter out of range!");
|
---|
| 622 | if (A.IsEmpty)
|
---|
| 623 | return new ILRetArray<float>(A.Size);
|
---|
| 624 | if (A.IsScalar) {
|
---|
| 625 |
|
---|
| 626 | return new ILRetArray<float>(new float[] { A.GetValue(0) }, 1, 1);
|
---|
| 627 | }
|
---|
| 628 | ILSize inDim = A.Size;
|
---|
| 629 | int[] newDims = inDim.ToIntArray();
|
---|
| 630 |
|
---|
| 631 | if (inDim[dim] == 1) return A.C;
|
---|
| 632 |
|
---|
| 633 | int newLength;
|
---|
| 634 |
|
---|
| 635 | float[] retArr;
|
---|
| 636 | // build ILSize
|
---|
| 637 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
| 638 | newDims[dim] = 1;
|
---|
| 639 | retArr = ILMemoryPool.Pool.New< float>(newLength);
|
---|
| 640 | ILSize newDimension = new ILSize(newDims);
|
---|
| 641 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
| 642 | int dimLen = inDim[dim];
|
---|
| 643 | int nrHigherDims = inDim.NumberOfElements / dimLen;
|
---|
| 644 | if (dim == 0) {
|
---|
| 645 | #region physical along 1st leading dimension
|
---|
| 646 | unsafe {
|
---|
| 647 | fixed ( float* pOutArr = retArr)
|
---|
| 648 | fixed ( float* pInArr = A.GetArrayForRead()) {
|
---|
| 649 |
|
---|
| 650 | float* lastElement;
|
---|
| 651 |
|
---|
| 652 | float* tmpOut = pOutArr;
|
---|
| 653 |
|
---|
| 654 | float* tmpIn = pInArr;
|
---|
| 655 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 656 | lastElement = tmpIn + dimLen;
|
---|
| 657 | *tmpOut = 0.0f;
|
---|
| 658 | while (tmpIn < lastElement) {
|
---|
| 659 |
|
---|
| 660 | float inVal = *(tmpIn++);
|
---|
| 661 |
|
---|
| 662 | /*dummy*/
|
---|
| 663 |
|
---|
| 664 | *tmpOut += (float) /*dummy*/ (inVal) ;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | /*dummy*/
|
---|
| 668 | tmpOut++;
|
---|
| 669 | }
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 | #endregion
|
---|
| 673 | } else {
|
---|
| 674 | #region physical along abitrary dimension
|
---|
| 675 | // sum along abitrary dimension
|
---|
| 676 | unsafe {
|
---|
| 677 | fixed ( float* pOutArr = retArr)
|
---|
| 678 | fixed ( float* pInArr = A.GetArrayForRead()) {
|
---|
| 679 |
|
---|
| 680 | float* lastElementOut = newLength + pOutArr - 1;
|
---|
| 681 | int inLength = inDim.NumberOfElements - 1;
|
---|
| 682 |
|
---|
| 683 | float* lastElementIn = pInArr + inLength;
|
---|
| 684 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
| 685 |
|
---|
| 686 | float* tmpOut = pOutArr;
|
---|
| 687 | int outLength = newLength - 1;
|
---|
| 688 |
|
---|
| 689 | float* leadEnd;
|
---|
| 690 |
|
---|
| 691 | float* tmpIn = pInArr;
|
---|
| 692 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 693 | leadEnd = tmpIn + dimLen * inc;
|
---|
| 694 | *tmpOut = 0.0f;
|
---|
| 695 | while (tmpIn < leadEnd) {
|
---|
| 696 |
|
---|
| 697 | float inVal = *(tmpIn);
|
---|
| 698 | tmpIn += inc;
|
---|
| 699 |
|
---|
| 700 | /*dummy*/
|
---|
| 701 |
|
---|
| 702 | *tmpOut += (float) /*dummy*/ (inVal) ; //
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | /*dummy*/
|
---|
| 706 | tmpOut += inc;
|
---|
| 707 | if (tmpOut > lastElementOut)
|
---|
| 708 | tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength);
|
---|
| 709 | if (tmpIn > lastElementIn)
|
---|
| 710 | tmpIn = pInArr + ((tmpIn - pInArr) - inLength);
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
| 713 | }
|
---|
| 714 | #endregion
|
---|
| 715 | }
|
---|
| 716 | return new ILRetArray<float>(retArr, newDims);
|
---|
| 717 | }
|
---|
| 718 | }
|
---|
| 719 | /// <summary>
|
---|
| 720 | /// Sum elements of A along dimension specified.
|
---|
| 721 | /// </summary>
|
---|
| 722 | /// <param name="A">Input array</param>
|
---|
| 723 | /// <param name="dim">[Optional] Index of the dimension to operate along. If omitted operates along the first non singleton dimension (i.e. != 1).</param>
|
---|
| 724 | /// <returns>Array, same size as A, but having the specified or first non singleton dimension reduced to the length 1 with the sum of all
|
---|
| 725 | /// elements along that dimension.</returns>
|
---|
| 726 | public static ILRetArray<complex> sum (ILInArray<complex> A, int dim = -1) {
|
---|
| 727 | using (ILScope.Enter(A)) {
|
---|
| 728 | if (dim < 0)
|
---|
| 729 | dim = A.Size.WorkingDimension();
|
---|
| 730 | if (dim >= A.Size.NumberOfDimensions)
|
---|
| 731 | throw new ILArgumentException("dimension parameter out of range!");
|
---|
| 732 | if (A.IsEmpty)
|
---|
| 733 | return new ILRetArray<complex>(A.Size);
|
---|
| 734 | if (A.IsScalar) {
|
---|
| 735 |
|
---|
| 736 | return new ILRetArray<complex>(new complex[] { A.GetValue(0) }, 1, 1);
|
---|
| 737 | }
|
---|
| 738 | ILSize inDim = A.Size;
|
---|
| 739 | int[] newDims = inDim.ToIntArray();
|
---|
| 740 |
|
---|
| 741 | if (inDim[dim] == 1) return A.C;
|
---|
| 742 |
|
---|
| 743 | int newLength;
|
---|
| 744 |
|
---|
| 745 | complex[] retArr;
|
---|
| 746 | // build ILSize
|
---|
| 747 | newLength = inDim.NumberOfElements / newDims[dim];
|
---|
| 748 | newDims[dim] = 1;
|
---|
| 749 | retArr = ILMemoryPool.Pool.New< complex>(newLength);
|
---|
| 750 | ILSize newDimension = new ILSize(newDims);
|
---|
| 751 | int incOut = newDimension.SequentialIndexDistance(dim);
|
---|
| 752 | int dimLen = inDim[dim];
|
---|
| 753 | int nrHigherDims = inDim.NumberOfElements / dimLen;
|
---|
| 754 | if (dim == 0) {
|
---|
| 755 | #region physical along 1st leading dimension
|
---|
| 756 | unsafe {
|
---|
| 757 | fixed ( complex* pOutArr = retArr)
|
---|
| 758 | fixed ( complex* pInArr = A.GetArrayForRead()) {
|
---|
| 759 |
|
---|
| 760 | complex* lastElement;
|
---|
| 761 |
|
---|
| 762 | complex* tmpOut = pOutArr;
|
---|
| 763 |
|
---|
| 764 | complex* tmpIn = pInArr;
|
---|
| 765 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 766 | lastElement = tmpIn + dimLen;
|
---|
| 767 | *tmpOut = 0.0;
|
---|
| 768 | while (tmpIn < lastElement) {
|
---|
| 769 |
|
---|
| 770 | complex inVal = *(tmpIn++);
|
---|
| 771 |
|
---|
| 772 | /*dummy*/
|
---|
| 773 |
|
---|
| 774 | *tmpOut += (complex) /*dummy*/ (inVal) ;
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | /*dummy*/
|
---|
| 778 | tmpOut++;
|
---|
| 779 | }
|
---|
| 780 | }
|
---|
| 781 | }
|
---|
| 782 | #endregion
|
---|
| 783 | } else {
|
---|
| 784 | #region physical along abitrary dimension
|
---|
| 785 | // sum along abitrary dimension
|
---|
| 786 | unsafe {
|
---|
| 787 | fixed ( complex* pOutArr = retArr)
|
---|
| 788 | fixed ( complex* pInArr = A.GetArrayForRead()) {
|
---|
| 789 |
|
---|
| 790 | complex* lastElementOut = newLength + pOutArr - 1;
|
---|
| 791 | int inLength = inDim.NumberOfElements - 1;
|
---|
| 792 |
|
---|
| 793 | complex* lastElementIn = pInArr + inLength;
|
---|
| 794 | int inc = inDim.SequentialIndexDistance(dim);
|
---|
| 795 |
|
---|
| 796 | complex* tmpOut = pOutArr;
|
---|
| 797 | int outLength = newLength - 1;
|
---|
| 798 |
|
---|
| 799 | complex* leadEnd;
|
---|
| 800 |
|
---|
| 801 | complex* tmpIn = pInArr;
|
---|
| 802 | for (int h = nrHigherDims; h-- > 0; ) {
|
---|
| 803 | leadEnd = tmpIn + dimLen * inc;
|
---|
| 804 | *tmpOut = 0.0;
|
---|
| 805 | while (tmpIn < leadEnd) {
|
---|
| 806 |
|
---|
| 807 | complex inVal = *(tmpIn);
|
---|
| 808 | tmpIn += inc;
|
---|
| 809 |
|
---|
| 810 | /*dummy*/
|
---|
| 811 |
|
---|
| 812 | *tmpOut += (complex) /*dummy*/ (inVal) ; //
|
---|
| 813 | }
|
---|
| 814 |
|
---|
| 815 | /*dummy*/
|
---|
| 816 | tmpOut += inc;
|
---|
| 817 | if (tmpOut > lastElementOut)
|
---|
| 818 | tmpOut = pOutArr + ((tmpOut - pOutArr) - outLength);
|
---|
| 819 | if (tmpIn > lastElementIn)
|
---|
| 820 | tmpIn = pInArr + ((tmpIn - pInArr) - inLength);
|
---|
| 821 | }
|
---|
| 822 | }
|
---|
| 823 | }
|
---|
| 824 | #endregion
|
---|
| 825 | }
|
---|
| 826 | return new ILRetArray<complex>(retArr, newDims);
|
---|
| 827 | }
|
---|
| 828 | }
|
---|
| 829 |
|
---|
| 830 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
| 831 | }
|
---|
| 832 | } |
---|