Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Functions/ILMath_General.cs @ 11219

Last change on this file since 11219 was 9102, checked in by gkronber, 11 years ago

#1967: ILNumerics source for experimentation

File size: 8.0 KB
Line 
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
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.Runtime.InteropServices;
44using ILNumerics.Storage;
45using ILNumerics.Misc;
46using ILNumerics.Native;
47using ILNumerics.Exceptions;
48using System.IO;
49
50namespace ILNumerics {
51
52    /// <summary>
53    /// Main math class, exposes all static math functions. Users should write algorithms in a class derived from <c>ILMath</c>.
54    /// </summary>
55    public partial class ILMath {
56    /// <summary>
57    /// Concrete interface wrapper class providing the native LAPACK functions
58    /// </summary>
59    /// <remarks>The LAPACK wrapper will be initialized the first time,
60    /// a call to any static method of ILMath is made. The decision, which
61    /// native module to load is done by use of native CPUID assembly statements.
62    /// If the current processor does not support those calls or is not recognizable
63    /// by ILNumerics, a generic - not optimized - version of native LAPACK code will
64    /// be used than.</remarks>
65        public static IILLapack Lapack;
66
67        /// <summary>
68        /// Platform specific FFT implementation, <b>internally</b> used to compute fft
69        /// </summary>
70        public static IILFFT FFTImplementation;
71       
72        /// <summary>
73        /// Main math class providing static builtin functions
74        /// </summary>
75        static ILMath() {
76            #region initialize proc specific interfaces
77            // for now we rely on the existance of the Intel MKL!
78            try {
79                ILMKLFFT mkl = new ILMKLFFT();
80                FFTImplementation = mkl;
81                ILLapackMKL10_0 lapack = new ILLapackMKL10_0();
82                Lapack = lapack;
83                fft(1.0);
84            } catch (Exception exc) {
85                Console.Write("Error initializing Lapack implementation: " + exc.ToString());
86                // MKL missing ... no fallback by now
87            }
88            #endregion
89            #region initialize machine parameter infos
90            macharD(ref m_machparDouble.ibeta, ref m_machparDouble.it,ref m_machparDouble.irnd,ref m_machparDouble.ngrd,ref m_machparDouble.machep,ref m_machparDouble.negep,ref m_machparDouble.iexp,ref m_machparDouble.minexp,ref m_machparDouble.maxexp,ref m_machparDouble.eps,ref m_machparDouble.epsneg,ref m_machparDouble.xmin,ref m_machparDouble.xmax);
91            macharF(ref m_machparSingle.ibeta, ref m_machparSingle.it,ref m_machparSingle.irnd,ref m_machparSingle.ngrd,ref m_machparSingle.machep,ref m_machparSingle.negep,ref m_machparSingle.iexp,ref m_machparSingle.minexp,ref m_machparSingle.maxexp,ref m_machparSingle.eps,ref m_machparSingle.epsneg,ref m_machparSingle.xmin,ref m_machparSingle.xmax);
92            #endregion
93
94        }
95    /// <summary>
96    /// Minimal size of dimensions, expensive operations will be carried out by native LAPACK libs.
97    /// </summary>
98    /// <remarks>This property is not yet implemented. All computations (unless for scalars) will be using LAPACK.
99    /// </remarks>
100        internal static readonly int ILAtlasMinimumElementSize = 1;
101
102        #region private helper
103        private static readonly char TRANS_NONE = 'n';
104
105        private static byte saturateByte(double a) {
106            if (double.IsNaN(a)) return 0;
107            a = Math.Round(a, MidpointRounding.AwayFromZero);
108            if (a > byte.MaxValue) return byte.MaxValue;
109            if (a < byte.MinValue) return byte.MinValue;
110            return (byte)a;
111        }
112        private static sbyte saturateSByte(double a) {
113            if (double.IsNaN(a)) return 0;
114            a = Math.Round(a, MidpointRounding.AwayFromZero);
115            if (a > sbyte.MaxValue) return sbyte.MaxValue;
116            if (a < sbyte.MinValue) return sbyte.MinValue;
117            return (sbyte)a;
118        }
119        private static char saturateChar(double a) {
120            if (double.IsNaN(a)) return (char)0;
121            a = Math.Round(a, MidpointRounding.AwayFromZero);
122            if (a > char.MaxValue) return char.MaxValue;
123            if (a < char.MinValue) return char.MinValue;
124            return (char)a;
125        }
126        private static short saturateInt16(double a) {
127            if (double.IsNaN(a)) return 0;
128            a = Math.Round(a, MidpointRounding.AwayFromZero);
129            if (a > short.MaxValue) return short.MaxValue;
130            if (a < short.MinValue) return short.MinValue;
131            return (short)a;
132        }
133        private static int saturateInt32(double a) {
134            if (double.IsNaN(a)) return 0;
135            a = Math.Round(a, MidpointRounding.AwayFromZero);
136            if (a > int.MaxValue) return int.MaxValue;
137            if (a < int.MinValue) return int.MinValue;
138            return (int)a;
139        }
140        private static long saturateInt64(double a) {
141            if (double.IsNaN(a)) return 0;
142            a = Math.Round(a, MidpointRounding.AwayFromZero);
143            if (a > long.MaxValue) return long.MaxValue;
144            if (a < long.MinValue) return long.MinValue;
145            return (long)a;
146        }
147        private static ushort saturateUInt16(double a) {
148            if (double.IsNaN(a)) return 0;
149            a = Math.Round(a, MidpointRounding.AwayFromZero);
150            if (a > ushort.MaxValue) return ushort.MaxValue;
151            if (a < ushort.MinValue) return ushort.MinValue;
152            return (ushort)a;
153        }
154        private static uint saturateUInt32(double a) {
155            if (double.IsNaN(a)) return 0;
156            a = Math.Round(a, MidpointRounding.AwayFromZero);
157            if (a > uint.MaxValue) return uint.MaxValue;
158            if (a < uint.MinValue) return uint.MinValue;
159            return (uint)a;
160        }
161        private static ulong saturateUInt64(double a) {
162            if (double.IsNaN(a)) return 0;
163            a = Math.Round(a, MidpointRounding.AwayFromZero);
164            if (a > ulong.MaxValue) return ulong.MaxValue;
165            if (a < ulong.MinValue) return ulong.MinValue;
166            return (ulong)a;
167        }
168
169        #endregion
170    }
171}
Note: See TracBrowser for help on using the repository browser.