[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;
|
---|
| 44 | using ILNumerics.Exceptions;
|
---|
| 45 | using ILNumerics.Storage;
|
---|
| 46 | using ILNumerics.Misc;
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | namespace ILNumerics {
|
---|
| 51 |
|
---|
| 52 | public partial class ILMath {
|
---|
| 53 | /// <summary>
|
---|
| 54 | /// Find out, if value is finite
|
---|
| 55 | /// </summary>
|
---|
| 56 | /// <param name="input">Input value</param>
|
---|
| 57 | /// <returns>True for finite values</returns>
|
---|
| 58 | internal static bool isfinite(double input) {
|
---|
| 59 | if (!double.IsInfinity(input) && !double.IsNaN(input))
|
---|
| 60 | return true;
|
---|
| 61 | else
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 | /// <summary>
|
---|
| 65 | /// Find out, if value is finite
|
---|
| 66 | /// </summary>
|
---|
| 67 | /// <param name="input">Input value</param>
|
---|
| 68 | /// <returns>True for finite values</returns>
|
---|
| 69 | internal static bool isfinite(complex input) {
|
---|
| 70 | if (!complex.IsInfinity(input) && !complex.IsNaN(input))
|
---|
| 71 | return true;
|
---|
| 72 | else
|
---|
| 73 | return false;
|
---|
| 74 | }
|
---|
| 75 | /// <summary>
|
---|
| 76 | /// Find out, if value is finite
|
---|
| 77 | /// </summary>
|
---|
| 78 | /// <param name="input">Input value</param>
|
---|
| 79 | /// <returns>True for finite values</returns>
|
---|
| 80 | internal static bool isfinite(float input) {
|
---|
| 81 | if (!float.IsInfinity(input) && !float.IsNaN(input))
|
---|
| 82 | return true;
|
---|
| 83 | else
|
---|
| 84 | return false;
|
---|
| 85 | }
|
---|
| 86 | /// <summary>
|
---|
| 87 | /// Find out, if value is finite
|
---|
| 88 | /// </summary>
|
---|
| 89 | /// <param name="input">Input value</param>
|
---|
| 90 | /// <returns>True for finite values</returns>
|
---|
| 91 | internal static bool isfinite(fcomplex input) {
|
---|
| 92 | if (!fcomplex.IsInfinity(input) && !fcomplex.IsNaN(input))
|
---|
| 93 | return true;
|
---|
| 94 | else
|
---|
| 95 | return false;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | |
---|
| 99 |
|
---|
| 100 | |
---|
| 101 | #region HYCALPER AUTO GENERATED CODE
|
---|
| 102 | |
---|
| 103 | /// <summary>Finds finite value elements</summary>
|
---|
| 104 | /// <param name="A">Input array</param>
|
---|
| 105 | /// <returns>Logical array with 1 if the corresponding elements of input array is finite, 0 else.</returns>
|
---|
| 106 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 107 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 108 | public unsafe static ILRetLogical isfinite (ILInArray< double > A) {
|
---|
| 109 | using (ILScope.Enter(A)) {
|
---|
| 110 | if (A.IsEmpty)
|
---|
| 111 | return new ILRetLogical(A.Size);
|
---|
| 112 | ILSize inDim = A.Size;
|
---|
| 113 | double[] arrA = A.GetArrayForRead();
|
---|
| 114 | byte [] retArr;
|
---|
| 115 | int outLen = inDim.NumberOfElements;
|
---|
| 116 | bool inplace = true;
|
---|
| 117 |
|
---|
| 118 | if (true){
|
---|
| 119 | retArr = ILMemoryPool.Pool.New<byte>(outLen);
|
---|
| 120 | inplace = false;
|
---|
| 121 | }
|
---|
| 122 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 123 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 124 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 125 | workItemLength = outLen / workItemCount;
|
---|
| 126 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 127 | } else {
|
---|
| 128 | workItemLength = outLen / 2;
|
---|
| 129 | workItemCount = 2;
|
---|
| 130 | }
|
---|
| 131 | } else {
|
---|
| 132 | workItemLength = outLen;
|
---|
| 133 | workItemCount = 1;
|
---|
| 134 | }
|
---|
| 135 | ILDenseStorage<byte> retStorage = new ILDenseStorage<byte>(retArr, inDim);
|
---|
| 136 |
|
---|
| 137 | Action<object> worker = data => {
|
---|
| 138 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 139 |
|
---|
| 140 | byte* cp = ((byte*)range.Item4 + range.Item1);
|
---|
| 141 | int len = range.Item2;
|
---|
| 142 | if (range.Item5) {
|
---|
| 143 | // inplace
|
---|
| 144 | while (len > 20) {
|
---|
| 145 | cp[0] = isfinite(cp[0] ) ?(byte)1:(byte)0;;
|
---|
| 146 | cp[1] = isfinite(cp[1] ) ?(byte)1:(byte)0;;
|
---|
| 147 | cp[2] = isfinite(cp[2] ) ?(byte)1:(byte)0;;
|
---|
| 148 | cp[3] = isfinite(cp[3] ) ?(byte)1:(byte)0;;
|
---|
| 149 | cp[4] = isfinite(cp[4] ) ?(byte)1:(byte)0;;
|
---|
| 150 | cp[5] = isfinite(cp[5] ) ?(byte)1:(byte)0;;
|
---|
| 151 | cp[6] = isfinite(cp[6] ) ?(byte)1:(byte)0;;
|
---|
| 152 | cp[7] = isfinite(cp[7] ) ?(byte)1:(byte)0;;
|
---|
| 153 | cp[8] = isfinite(cp[8] ) ?(byte)1:(byte)0;;
|
---|
| 154 | cp[9] = isfinite(cp[9] ) ?(byte)1:(byte)0;;
|
---|
| 155 | cp[10] = isfinite(cp[10] ) ?(byte)1:(byte)0;;
|
---|
| 156 | cp[11] = isfinite(cp[11] ) ?(byte)1:(byte)0;;
|
---|
| 157 | cp[12] = isfinite(cp[12] ) ?(byte)1:(byte)0;;
|
---|
| 158 | cp[13] = isfinite(cp[13] ) ?(byte)1:(byte)0;;
|
---|
| 159 | cp[14] = isfinite(cp[14] ) ?(byte)1:(byte)0;;
|
---|
| 160 | cp[15] = isfinite(cp[15] ) ?(byte)1:(byte)0;;
|
---|
| 161 | cp[16] = isfinite(cp[16] ) ?(byte)1:(byte)0;;
|
---|
| 162 | cp[17] = isfinite(cp[17] ) ?(byte)1:(byte)0;;
|
---|
| 163 | cp[18] = isfinite(cp[18] ) ?(byte)1:(byte)0;;
|
---|
| 164 | cp[19] = isfinite(cp[19] ) ?(byte)1:(byte)0;;
|
---|
| 165 | cp[20] = isfinite(cp[20] ) ?(byte)1:(byte)0;;
|
---|
| 166 | cp+=21; len -= 21;
|
---|
| 167 | }
|
---|
| 168 | while (len-- > 0) {
|
---|
| 169 | *cp = isfinite(*cp ) ?(byte)1:(byte)0;;
|
---|
| 170 | cp++;
|
---|
| 171 | }
|
---|
| 172 | } else {
|
---|
| 173 | double* ap = ((double*)range.Item3 + range.Item1);
|
---|
| 174 | while (len > 20) {
|
---|
| 175 | cp[0] = isfinite(ap[0] ) ?(byte)1:(byte)0;;
|
---|
| 176 | cp[1] = isfinite(ap[1] ) ?(byte)1:(byte)0;;
|
---|
| 177 | cp[2] = isfinite(ap[2] ) ?(byte)1:(byte)0;;
|
---|
| 178 | cp[3] = isfinite(ap[3] ) ?(byte)1:(byte)0;;
|
---|
| 179 | cp[4] = isfinite(ap[4] ) ?(byte)1:(byte)0;;
|
---|
| 180 | cp[5] = isfinite(ap[5] ) ?(byte)1:(byte)0;;
|
---|
| 181 | cp[6] = isfinite(ap[6] ) ?(byte)1:(byte)0;;
|
---|
| 182 | cp[7] = isfinite(ap[7] ) ?(byte)1:(byte)0;;
|
---|
| 183 | cp[8] = isfinite(ap[8] ) ?(byte)1:(byte)0;;
|
---|
| 184 | cp[9] = isfinite(ap[9] ) ?(byte)1:(byte)0;;
|
---|
| 185 | cp[10] = isfinite(ap[10] ) ?(byte)1:(byte)0;;
|
---|
| 186 | cp[11] = isfinite(ap[11] ) ?(byte)1:(byte)0;;
|
---|
| 187 | cp[12] = isfinite(ap[12] ) ?(byte)1:(byte)0;;
|
---|
| 188 | cp[13] = isfinite(ap[13] ) ?(byte)1:(byte)0;;
|
---|
| 189 | cp[14] = isfinite(ap[14] ) ?(byte)1:(byte)0;;
|
---|
| 190 | cp[15] = isfinite(ap[15] ) ?(byte)1:(byte)0;;
|
---|
| 191 | cp[16] = isfinite(ap[16] ) ?(byte)1:(byte)0;;
|
---|
| 192 | cp[17] = isfinite(ap[17] ) ?(byte)1:(byte)0;;
|
---|
| 193 | cp[18] = isfinite(ap[18] ) ?(byte)1:(byte)0;;
|
---|
| 194 | cp[19] = isfinite(ap[19] ) ?(byte)1:(byte)0;;
|
---|
| 195 | cp[20] = isfinite(ap[20] ) ?(byte)1:(byte)0;;
|
---|
| 196 | ap += 21;
|
---|
| 197 | cp += 21;
|
---|
| 198 | len -= 21;
|
---|
| 199 | }
|
---|
| 200 | while (len-- > 0) {
|
---|
| 201 | *cp = isfinite(*ap ) ?(byte)1:(byte)0;;
|
---|
| 202 | ap++;
|
---|
| 203 | cp++;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 207 | };
|
---|
| 208 |
|
---|
| 209 | fixed ( double* arrAP = arrA)
|
---|
| 210 | fixed ( byte* retArrP = retArr) {
|
---|
| 211 | for (; i < workItemCount - 1; i++) {
|
---|
| 212 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 213 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 214 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 215 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 216 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 217 | }
|
---|
| 218 | // the last (or may the only) chunk is done right here
|
---|
| 219 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 220 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 221 |
|
---|
| 222 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 223 | }
|
---|
| 224 | return new ILRetLogical(retStorage);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | /// <summary>Finds finite value elements</summary>
|
---|
| 228 | /// <param name="A">Input array</param>
|
---|
| 229 | /// <returns>Logical array with 1 if the corresponding elements of input array is finite, 0 else.</returns>
|
---|
| 230 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 231 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 232 | public unsafe static ILRetLogical isfinite (ILInArray< float > A) {
|
---|
| 233 | using (ILScope.Enter(A)) {
|
---|
| 234 | if (A.IsEmpty)
|
---|
| 235 | return new ILRetLogical(A.Size);
|
---|
| 236 | ILSize inDim = A.Size;
|
---|
| 237 | float[] arrA = A.GetArrayForRead();
|
---|
| 238 | byte [] retArr;
|
---|
| 239 | int outLen = inDim.NumberOfElements;
|
---|
| 240 | bool inplace = true;
|
---|
| 241 |
|
---|
| 242 | if (true){
|
---|
| 243 | retArr = ILMemoryPool.Pool.New<byte>(outLen);
|
---|
| 244 | inplace = false;
|
---|
| 245 | }
|
---|
| 246 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 247 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 248 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 249 | workItemLength = outLen / workItemCount;
|
---|
| 250 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 251 | } else {
|
---|
| 252 | workItemLength = outLen / 2;
|
---|
| 253 | workItemCount = 2;
|
---|
| 254 | }
|
---|
| 255 | } else {
|
---|
| 256 | workItemLength = outLen;
|
---|
| 257 | workItemCount = 1;
|
---|
| 258 | }
|
---|
| 259 | ILDenseStorage<byte> retStorage = new ILDenseStorage<byte>(retArr, inDim);
|
---|
| 260 |
|
---|
| 261 | Action<object> worker = data => {
|
---|
| 262 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 263 |
|
---|
| 264 | byte* cp = ((byte*)range.Item4 + range.Item1);
|
---|
| 265 | int len = range.Item2;
|
---|
| 266 | if (range.Item5) {
|
---|
| 267 | // inplace
|
---|
| 268 | while (len > 20) {
|
---|
| 269 | cp[0] = isfinite(cp[0] ) ?(byte)1:(byte)0;;
|
---|
| 270 | cp[1] = isfinite(cp[1] ) ?(byte)1:(byte)0;;
|
---|
| 271 | cp[2] = isfinite(cp[2] ) ?(byte)1:(byte)0;;
|
---|
| 272 | cp[3] = isfinite(cp[3] ) ?(byte)1:(byte)0;;
|
---|
| 273 | cp[4] = isfinite(cp[4] ) ?(byte)1:(byte)0;;
|
---|
| 274 | cp[5] = isfinite(cp[5] ) ?(byte)1:(byte)0;;
|
---|
| 275 | cp[6] = isfinite(cp[6] ) ?(byte)1:(byte)0;;
|
---|
| 276 | cp[7] = isfinite(cp[7] ) ?(byte)1:(byte)0;;
|
---|
| 277 | cp[8] = isfinite(cp[8] ) ?(byte)1:(byte)0;;
|
---|
| 278 | cp[9] = isfinite(cp[9] ) ?(byte)1:(byte)0;;
|
---|
| 279 | cp[10] = isfinite(cp[10] ) ?(byte)1:(byte)0;;
|
---|
| 280 | cp[11] = isfinite(cp[11] ) ?(byte)1:(byte)0;;
|
---|
| 281 | cp[12] = isfinite(cp[12] ) ?(byte)1:(byte)0;;
|
---|
| 282 | cp[13] = isfinite(cp[13] ) ?(byte)1:(byte)0;;
|
---|
| 283 | cp[14] = isfinite(cp[14] ) ?(byte)1:(byte)0;;
|
---|
| 284 | cp[15] = isfinite(cp[15] ) ?(byte)1:(byte)0;;
|
---|
| 285 | cp[16] = isfinite(cp[16] ) ?(byte)1:(byte)0;;
|
---|
| 286 | cp[17] = isfinite(cp[17] ) ?(byte)1:(byte)0;;
|
---|
| 287 | cp[18] = isfinite(cp[18] ) ?(byte)1:(byte)0;;
|
---|
| 288 | cp[19] = isfinite(cp[19] ) ?(byte)1:(byte)0;;
|
---|
| 289 | cp[20] = isfinite(cp[20] ) ?(byte)1:(byte)0;;
|
---|
| 290 | cp+=21; len -= 21;
|
---|
| 291 | }
|
---|
| 292 | while (len-- > 0) {
|
---|
| 293 | *cp = isfinite(*cp ) ?(byte)1:(byte)0;;
|
---|
| 294 | cp++;
|
---|
| 295 | }
|
---|
| 296 | } else {
|
---|
| 297 | float* ap = ((float*)range.Item3 + range.Item1);
|
---|
| 298 | while (len > 20) {
|
---|
| 299 | cp[0] = isfinite(ap[0] ) ?(byte)1:(byte)0;;
|
---|
| 300 | cp[1] = isfinite(ap[1] ) ?(byte)1:(byte)0;;
|
---|
| 301 | cp[2] = isfinite(ap[2] ) ?(byte)1:(byte)0;;
|
---|
| 302 | cp[3] = isfinite(ap[3] ) ?(byte)1:(byte)0;;
|
---|
| 303 | cp[4] = isfinite(ap[4] ) ?(byte)1:(byte)0;;
|
---|
| 304 | cp[5] = isfinite(ap[5] ) ?(byte)1:(byte)0;;
|
---|
| 305 | cp[6] = isfinite(ap[6] ) ?(byte)1:(byte)0;;
|
---|
| 306 | cp[7] = isfinite(ap[7] ) ?(byte)1:(byte)0;;
|
---|
| 307 | cp[8] = isfinite(ap[8] ) ?(byte)1:(byte)0;;
|
---|
| 308 | cp[9] = isfinite(ap[9] ) ?(byte)1:(byte)0;;
|
---|
| 309 | cp[10] = isfinite(ap[10] ) ?(byte)1:(byte)0;;
|
---|
| 310 | cp[11] = isfinite(ap[11] ) ?(byte)1:(byte)0;;
|
---|
| 311 | cp[12] = isfinite(ap[12] ) ?(byte)1:(byte)0;;
|
---|
| 312 | cp[13] = isfinite(ap[13] ) ?(byte)1:(byte)0;;
|
---|
| 313 | cp[14] = isfinite(ap[14] ) ?(byte)1:(byte)0;;
|
---|
| 314 | cp[15] = isfinite(ap[15] ) ?(byte)1:(byte)0;;
|
---|
| 315 | cp[16] = isfinite(ap[16] ) ?(byte)1:(byte)0;;
|
---|
| 316 | cp[17] = isfinite(ap[17] ) ?(byte)1:(byte)0;;
|
---|
| 317 | cp[18] = isfinite(ap[18] ) ?(byte)1:(byte)0;;
|
---|
| 318 | cp[19] = isfinite(ap[19] ) ?(byte)1:(byte)0;;
|
---|
| 319 | cp[20] = isfinite(ap[20] ) ?(byte)1:(byte)0;;
|
---|
| 320 | ap += 21;
|
---|
| 321 | cp += 21;
|
---|
| 322 | len -= 21;
|
---|
| 323 | }
|
---|
| 324 | while (len-- > 0) {
|
---|
| 325 | *cp = isfinite(*ap ) ?(byte)1:(byte)0;;
|
---|
| 326 | ap++;
|
---|
| 327 | cp++;
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 331 | };
|
---|
| 332 |
|
---|
| 333 | fixed ( float* arrAP = arrA)
|
---|
| 334 | fixed ( byte* retArrP = retArr) {
|
---|
| 335 | for (; i < workItemCount - 1; i++) {
|
---|
| 336 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 337 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 338 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 339 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 340 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 341 | }
|
---|
| 342 | // the last (or may the only) chunk is done right here
|
---|
| 343 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 344 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 345 |
|
---|
| 346 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 347 | }
|
---|
| 348 | return new ILRetLogical(retStorage);
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | /// <summary>Finds finite value elements</summary>
|
---|
| 352 | /// <param name="A">Input array</param>
|
---|
| 353 | /// <returns>Logical array with 1 if the corresponding elements of input array is finite, 0 else.</returns>
|
---|
| 354 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 355 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 356 | public unsafe static ILRetLogical isfinite (ILInArray< fcomplex > A) {
|
---|
| 357 | using (ILScope.Enter(A)) {
|
---|
| 358 | if (A.IsEmpty)
|
---|
| 359 | return new ILRetLogical(A.Size);
|
---|
| 360 | ILSize inDim = A.Size;
|
---|
| 361 | fcomplex[] arrA = A.GetArrayForRead();
|
---|
| 362 | byte [] retArr;
|
---|
| 363 | int outLen = inDim.NumberOfElements;
|
---|
| 364 | bool inplace = true;
|
---|
| 365 |
|
---|
| 366 | if (true){
|
---|
| 367 | retArr = ILMemoryPool.Pool.New<byte>(outLen);
|
---|
| 368 | inplace = false;
|
---|
| 369 | }
|
---|
| 370 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 371 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 372 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 373 | workItemLength = outLen / workItemCount;
|
---|
| 374 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 375 | } else {
|
---|
| 376 | workItemLength = outLen / 2;
|
---|
| 377 | workItemCount = 2;
|
---|
| 378 | }
|
---|
| 379 | } else {
|
---|
| 380 | workItemLength = outLen;
|
---|
| 381 | workItemCount = 1;
|
---|
| 382 | }
|
---|
| 383 | ILDenseStorage<byte> retStorage = new ILDenseStorage<byte>(retArr, inDim);
|
---|
| 384 |
|
---|
| 385 | Action<object> worker = data => {
|
---|
| 386 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 387 |
|
---|
| 388 | byte* cp = ((byte*)range.Item4 + range.Item1);
|
---|
| 389 | int len = range.Item2;
|
---|
| 390 | if (range.Item5) {
|
---|
| 391 | // inplace
|
---|
| 392 | while (len > 20) {
|
---|
| 393 | cp[0] = fcomplex.IsFinite(cp[0] ) ?(byte)1:(byte)0;;
|
---|
| 394 | cp[1] = fcomplex.IsFinite(cp[1] ) ?(byte)1:(byte)0;;
|
---|
| 395 | cp[2] = fcomplex.IsFinite(cp[2] ) ?(byte)1:(byte)0;;
|
---|
| 396 | cp[3] = fcomplex.IsFinite(cp[3] ) ?(byte)1:(byte)0;;
|
---|
| 397 | cp[4] = fcomplex.IsFinite(cp[4] ) ?(byte)1:(byte)0;;
|
---|
| 398 | cp[5] = fcomplex.IsFinite(cp[5] ) ?(byte)1:(byte)0;;
|
---|
| 399 | cp[6] = fcomplex.IsFinite(cp[6] ) ?(byte)1:(byte)0;;
|
---|
| 400 | cp[7] = fcomplex.IsFinite(cp[7] ) ?(byte)1:(byte)0;;
|
---|
| 401 | cp[8] = fcomplex.IsFinite(cp[8] ) ?(byte)1:(byte)0;;
|
---|
| 402 | cp[9] = fcomplex.IsFinite(cp[9] ) ?(byte)1:(byte)0;;
|
---|
| 403 | cp[10] = fcomplex.IsFinite(cp[10] ) ?(byte)1:(byte)0;;
|
---|
| 404 | cp[11] = fcomplex.IsFinite(cp[11] ) ?(byte)1:(byte)0;;
|
---|
| 405 | cp[12] = fcomplex.IsFinite(cp[12] ) ?(byte)1:(byte)0;;
|
---|
| 406 | cp[13] = fcomplex.IsFinite(cp[13] ) ?(byte)1:(byte)0;;
|
---|
| 407 | cp[14] = fcomplex.IsFinite(cp[14] ) ?(byte)1:(byte)0;;
|
---|
| 408 | cp[15] = fcomplex.IsFinite(cp[15] ) ?(byte)1:(byte)0;;
|
---|
| 409 | cp[16] = fcomplex.IsFinite(cp[16] ) ?(byte)1:(byte)0;;
|
---|
| 410 | cp[17] = fcomplex.IsFinite(cp[17] ) ?(byte)1:(byte)0;;
|
---|
| 411 | cp[18] = fcomplex.IsFinite(cp[18] ) ?(byte)1:(byte)0;;
|
---|
| 412 | cp[19] = fcomplex.IsFinite(cp[19] ) ?(byte)1:(byte)0;;
|
---|
| 413 | cp[20] = fcomplex.IsFinite(cp[20] ) ?(byte)1:(byte)0;;
|
---|
| 414 | cp+=21; len -= 21;
|
---|
| 415 | }
|
---|
| 416 | while (len-- > 0) {
|
---|
| 417 | *cp = fcomplex.IsFinite(*cp ) ?(byte)1:(byte)0;;
|
---|
| 418 | cp++;
|
---|
| 419 | }
|
---|
| 420 | } else {
|
---|
| 421 | fcomplex* ap = ((fcomplex*)range.Item3 + range.Item1);
|
---|
| 422 | while (len > 20) {
|
---|
| 423 | cp[0] = fcomplex.IsFinite(ap[0] ) ?(byte)1:(byte)0;;
|
---|
| 424 | cp[1] = fcomplex.IsFinite(ap[1] ) ?(byte)1:(byte)0;;
|
---|
| 425 | cp[2] = fcomplex.IsFinite(ap[2] ) ?(byte)1:(byte)0;;
|
---|
| 426 | cp[3] = fcomplex.IsFinite(ap[3] ) ?(byte)1:(byte)0;;
|
---|
| 427 | cp[4] = fcomplex.IsFinite(ap[4] ) ?(byte)1:(byte)0;;
|
---|
| 428 | cp[5] = fcomplex.IsFinite(ap[5] ) ?(byte)1:(byte)0;;
|
---|
| 429 | cp[6] = fcomplex.IsFinite(ap[6] ) ?(byte)1:(byte)0;;
|
---|
| 430 | cp[7] = fcomplex.IsFinite(ap[7] ) ?(byte)1:(byte)0;;
|
---|
| 431 | cp[8] = fcomplex.IsFinite(ap[8] ) ?(byte)1:(byte)0;;
|
---|
| 432 | cp[9] = fcomplex.IsFinite(ap[9] ) ?(byte)1:(byte)0;;
|
---|
| 433 | cp[10] = fcomplex.IsFinite(ap[10] ) ?(byte)1:(byte)0;;
|
---|
| 434 | cp[11] = fcomplex.IsFinite(ap[11] ) ?(byte)1:(byte)0;;
|
---|
| 435 | cp[12] = fcomplex.IsFinite(ap[12] ) ?(byte)1:(byte)0;;
|
---|
| 436 | cp[13] = fcomplex.IsFinite(ap[13] ) ?(byte)1:(byte)0;;
|
---|
| 437 | cp[14] = fcomplex.IsFinite(ap[14] ) ?(byte)1:(byte)0;;
|
---|
| 438 | cp[15] = fcomplex.IsFinite(ap[15] ) ?(byte)1:(byte)0;;
|
---|
| 439 | cp[16] = fcomplex.IsFinite(ap[16] ) ?(byte)1:(byte)0;;
|
---|
| 440 | cp[17] = fcomplex.IsFinite(ap[17] ) ?(byte)1:(byte)0;;
|
---|
| 441 | cp[18] = fcomplex.IsFinite(ap[18] ) ?(byte)1:(byte)0;;
|
---|
| 442 | cp[19] = fcomplex.IsFinite(ap[19] ) ?(byte)1:(byte)0;;
|
---|
| 443 | cp[20] = fcomplex.IsFinite(ap[20] ) ?(byte)1:(byte)0;;
|
---|
| 444 | ap += 21;
|
---|
| 445 | cp += 21;
|
---|
| 446 | len -= 21;
|
---|
| 447 | }
|
---|
| 448 | while (len-- > 0) {
|
---|
| 449 | *cp = fcomplex.IsFinite(*ap ) ?(byte)1:(byte)0;;
|
---|
| 450 | ap++;
|
---|
| 451 | cp++;
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 455 | };
|
---|
| 456 |
|
---|
| 457 | fixed ( fcomplex* arrAP = arrA)
|
---|
| 458 | fixed ( byte* retArrP = retArr) {
|
---|
| 459 | for (; i < workItemCount - 1; i++) {
|
---|
| 460 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 461 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 462 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 463 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 464 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 465 | }
|
---|
| 466 | // the last (or may the only) chunk is done right here
|
---|
| 467 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 468 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 469 |
|
---|
| 470 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 471 | }
|
---|
| 472 | return new ILRetLogical(retStorage);
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 | /// <summary>Finds finite value elements</summary>
|
---|
| 476 | /// <param name="A">Input array</param>
|
---|
| 477 | /// <returns>Logical array with 1 if the corresponding elements of input array is finite, 0 else.</returns>
|
---|
| 478 | /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
|
---|
| 479 | /// <para>The array returned will be a dense array.</para></remarks>
|
---|
| 480 | public unsafe static ILRetLogical isfinite (ILInArray< complex > A) {
|
---|
| 481 | using (ILScope.Enter(A)) {
|
---|
| 482 | if (A.IsEmpty)
|
---|
| 483 | return new ILRetLogical(A.Size);
|
---|
| 484 | ILSize inDim = A.Size;
|
---|
| 485 | complex[] arrA = A.GetArrayForRead();
|
---|
| 486 | byte [] retArr;
|
---|
| 487 | int outLen = inDim.NumberOfElements;
|
---|
| 488 | bool inplace = true;
|
---|
| 489 |
|
---|
| 490 | if (true){
|
---|
| 491 | retArr = ILMemoryPool.Pool.New<byte>(outLen);
|
---|
| 492 | inplace = false;
|
---|
| 493 | }
|
---|
| 494 | int i = 0, workItemCount = Settings.s_maxNumberThreads, workItemLength, workerCount = 1;
|
---|
| 495 | if (Settings.s_maxNumberThreads > 1 && outLen / 2 > Settings.s_minParallelElement1Count) {
|
---|
| 496 | if (outLen / workItemCount > Settings.s_minParallelElement1Count) {
|
---|
| 497 | workItemLength = outLen / workItemCount;
|
---|
| 498 | //workItemLength = (int)((double)outLen / workItemCount * 1.05);
|
---|
| 499 | } else {
|
---|
| 500 | workItemLength = outLen / 2;
|
---|
| 501 | workItemCount = 2;
|
---|
| 502 | }
|
---|
| 503 | } else {
|
---|
| 504 | workItemLength = outLen;
|
---|
| 505 | workItemCount = 1;
|
---|
| 506 | }
|
---|
| 507 | ILDenseStorage<byte> retStorage = new ILDenseStorage<byte>(retArr, inDim);
|
---|
| 508 |
|
---|
| 509 | Action<object> worker = data => {
|
---|
| 510 | Tuple<int, int, IntPtr, IntPtr, bool> range = (Tuple<int, int, IntPtr, IntPtr, bool>)data;
|
---|
| 511 |
|
---|
| 512 | byte* cp = ((byte*)range.Item4 + range.Item1);
|
---|
| 513 | int len = range.Item2;
|
---|
| 514 | if (range.Item5) {
|
---|
| 515 | // inplace
|
---|
| 516 | while (len > 20) {
|
---|
| 517 | cp[0] = complex.IsFinite(cp[0] ) ?(byte)1:(byte)0;;
|
---|
| 518 | cp[1] = complex.IsFinite(cp[1] ) ?(byte)1:(byte)0;;
|
---|
| 519 | cp[2] = complex.IsFinite(cp[2] ) ?(byte)1:(byte)0;;
|
---|
| 520 | cp[3] = complex.IsFinite(cp[3] ) ?(byte)1:(byte)0;;
|
---|
| 521 | cp[4] = complex.IsFinite(cp[4] ) ?(byte)1:(byte)0;;
|
---|
| 522 | cp[5] = complex.IsFinite(cp[5] ) ?(byte)1:(byte)0;;
|
---|
| 523 | cp[6] = complex.IsFinite(cp[6] ) ?(byte)1:(byte)0;;
|
---|
| 524 | cp[7] = complex.IsFinite(cp[7] ) ?(byte)1:(byte)0;;
|
---|
| 525 | cp[8] = complex.IsFinite(cp[8] ) ?(byte)1:(byte)0;;
|
---|
| 526 | cp[9] = complex.IsFinite(cp[9] ) ?(byte)1:(byte)0;;
|
---|
| 527 | cp[10] = complex.IsFinite(cp[10] ) ?(byte)1:(byte)0;;
|
---|
| 528 | cp[11] = complex.IsFinite(cp[11] ) ?(byte)1:(byte)0;;
|
---|
| 529 | cp[12] = complex.IsFinite(cp[12] ) ?(byte)1:(byte)0;;
|
---|
| 530 | cp[13] = complex.IsFinite(cp[13] ) ?(byte)1:(byte)0;;
|
---|
| 531 | cp[14] = complex.IsFinite(cp[14] ) ?(byte)1:(byte)0;;
|
---|
| 532 | cp[15] = complex.IsFinite(cp[15] ) ?(byte)1:(byte)0;;
|
---|
| 533 | cp[16] = complex.IsFinite(cp[16] ) ?(byte)1:(byte)0;;
|
---|
| 534 | cp[17] = complex.IsFinite(cp[17] ) ?(byte)1:(byte)0;;
|
---|
| 535 | cp[18] = complex.IsFinite(cp[18] ) ?(byte)1:(byte)0;;
|
---|
| 536 | cp[19] = complex.IsFinite(cp[19] ) ?(byte)1:(byte)0;;
|
---|
| 537 | cp[20] = complex.IsFinite(cp[20] ) ?(byte)1:(byte)0;;
|
---|
| 538 | cp+=21; len -= 21;
|
---|
| 539 | }
|
---|
| 540 | while (len-- > 0) {
|
---|
| 541 | *cp = complex.IsFinite(*cp ) ?(byte)1:(byte)0;;
|
---|
| 542 | cp++;
|
---|
| 543 | }
|
---|
| 544 | } else {
|
---|
| 545 | complex* ap = ((complex*)range.Item3 + range.Item1);
|
---|
| 546 | while (len > 20) {
|
---|
| 547 | cp[0] = complex.IsFinite(ap[0] ) ?(byte)1:(byte)0;;
|
---|
| 548 | cp[1] = complex.IsFinite(ap[1] ) ?(byte)1:(byte)0;;
|
---|
| 549 | cp[2] = complex.IsFinite(ap[2] ) ?(byte)1:(byte)0;;
|
---|
| 550 | cp[3] = complex.IsFinite(ap[3] ) ?(byte)1:(byte)0;;
|
---|
| 551 | cp[4] = complex.IsFinite(ap[4] ) ?(byte)1:(byte)0;;
|
---|
| 552 | cp[5] = complex.IsFinite(ap[5] ) ?(byte)1:(byte)0;;
|
---|
| 553 | cp[6] = complex.IsFinite(ap[6] ) ?(byte)1:(byte)0;;
|
---|
| 554 | cp[7] = complex.IsFinite(ap[7] ) ?(byte)1:(byte)0;;
|
---|
| 555 | cp[8] = complex.IsFinite(ap[8] ) ?(byte)1:(byte)0;;
|
---|
| 556 | cp[9] = complex.IsFinite(ap[9] ) ?(byte)1:(byte)0;;
|
---|
| 557 | cp[10] = complex.IsFinite(ap[10] ) ?(byte)1:(byte)0;;
|
---|
| 558 | cp[11] = complex.IsFinite(ap[11] ) ?(byte)1:(byte)0;;
|
---|
| 559 | cp[12] = complex.IsFinite(ap[12] ) ?(byte)1:(byte)0;;
|
---|
| 560 | cp[13] = complex.IsFinite(ap[13] ) ?(byte)1:(byte)0;;
|
---|
| 561 | cp[14] = complex.IsFinite(ap[14] ) ?(byte)1:(byte)0;;
|
---|
| 562 | cp[15] = complex.IsFinite(ap[15] ) ?(byte)1:(byte)0;;
|
---|
| 563 | cp[16] = complex.IsFinite(ap[16] ) ?(byte)1:(byte)0;;
|
---|
| 564 | cp[17] = complex.IsFinite(ap[17] ) ?(byte)1:(byte)0;;
|
---|
| 565 | cp[18] = complex.IsFinite(ap[18] ) ?(byte)1:(byte)0;;
|
---|
| 566 | cp[19] = complex.IsFinite(ap[19] ) ?(byte)1:(byte)0;;
|
---|
| 567 | cp[20] = complex.IsFinite(ap[20] ) ?(byte)1:(byte)0;;
|
---|
| 568 | ap += 21;
|
---|
| 569 | cp += 21;
|
---|
| 570 | len -= 21;
|
---|
| 571 | }
|
---|
| 572 | while (len-- > 0) {
|
---|
| 573 | *cp = complex.IsFinite(*ap ) ?(byte)1:(byte)0;;
|
---|
| 574 | ap++;
|
---|
| 575 | cp++;
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
| 578 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
| 579 | };
|
---|
| 580 |
|
---|
| 581 | fixed ( complex* arrAP = arrA)
|
---|
| 582 | fixed ( byte* retArrP = retArr) {
|
---|
| 583 | for (; i < workItemCount - 1; i++) {
|
---|
| 584 | Tuple<int, int, IntPtr, IntPtr, bool> range
|
---|
| 585 | = new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 586 | (i * workItemLength, workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace);
|
---|
| 587 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
| 588 | ILThreadPool.QueueUserWorkItem(i,worker, range);
|
---|
| 589 | }
|
---|
| 590 | // the last (or may the only) chunk is done right here
|
---|
| 591 | worker(new Tuple<int, int, IntPtr, IntPtr, bool>
|
---|
| 592 | (i * workItemLength, outLen - i * workItemLength, (IntPtr)arrAP, (IntPtr)retArrP, inplace));
|
---|
| 593 |
|
---|
| 594 | ILThreadPool.Wait4Workers(ref workerCount);
|
---|
| 595 | }
|
---|
| 596 | return new ILRetLogical(retStorage);
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
| 601 |
|
---|
| 602 | }
|
---|
| 603 | } |
---|