Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Array/ILBaseLogical.cs @ 10217

Last change on this file since 10217 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 11.8 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 ILNumerics;
42using ILNumerics.Misc;
43using ILNumerics.Exceptions;
44using ILNumerics.Storage;
45using ILNumerics.Native;
46using System.Text;
47
48namespace ILNumerics {
49  /// <summary>
50  /// Boolean array for high performance relational operations on arbitrary arrays
51  /// </summary>
52  /// <remarks>
53  /// Logical arrays are derived from <![CDATA[ILArray<byte>]]>. It consumes
54  /// 1 byte per element and is the output parameter of all relational comparisons
55  /// as well as the input parameter for all functions consuming <![CDATA[ILArray<byte>]]>.
56    /// The difference between <![CDATA[<byte>]]> and an ILLogical is, the ILLogical
57    /// storing a integer value with the number of nonzero elements as additional information.
58    /// Therefore functions like 'find' are able to determine the lenght of output array to
59    /// be created omitting the need of multiple walks through the array. Therefore ILLogicalArrays
60    /// consume (a little) more time while construction but are much more performand on functions like
61    /// 'find'.
62  /// </remarks>
63    [Serializable]
64    public class ILBaseLogical : ILDenseArray<byte> {
65
66        #region properties
67        /// <summary>
68        /// Number of 'true' elements in this array
69        /// </summary>
70        /// <remarks>This value caches the number of 'true' elements in this logical array.
71        /// It may be used for information purposes but is actually needed internally for performance
72        /// reasons.</remarks>
73        public virtual long NumberNonZero {
74            get {
75                return Storage.NumberNonZero;
76            }
77            internal set {
78                Storage.NumberNonZero = value;
79            }
80        }
81        /// <summary>
82        /// Shift the dimensions of this array by one (transpose for matrix)
83        /// </summary>
84        public new ILRetLogical T {
85            get {
86                return new ILRetLogical((ILLogicalStorage)Storage.ShiftDimensions(1), NumberNonZero);
87            }
88        }
89        internal new ILLogicalStorage Storage {
90            get {
91                return (m_storage as ILLogicalStorage);
92            }
93        }
94        /// <summary>
95        /// Create clone of this array
96        /// </summary>
97        public new ILRetLogical C {
98            get {
99                return new ILRetLogical((ILLogicalStorage)base.Clone().Storage, NumberNonZero);
100            }
101        }
102        #endregion
103
104        #region constructors
105        /// <summary>
106        /// Constructor creating ILLogical from dense storage
107        /// </summary>
108        /// <param name="A">Input array, the storage of this ILArray will directly be used for
109        /// storage of the new ILLogical</param>
110        /// <param name="isTempArray">Indicate whether the result is supposed to be a temporary array (true) or persistent (false)</param>
111    internal ILBaseLogical(ILDenseStorage<byte> A, bool isTempArray)
112      : base(A,isTempArray) {
113      NumberNonZero = sumElements();
114    }
115        #endregion
116
117        #region helper functions
118        /// <summary>
119        /// Sum all elements of this storage.
120        /// </summary>
121        /// <returns>Number of non zero elements</returns>
122        protected int sumElements() {
123            int ret = 0;
124            int nrElements = Storage.Size.NumberOfElements;
125            // physical storage
126            unsafe {
127                fixed (byte* pInArray = GetArrayForRead()) {
128                    byte* pCurData = pInArray;
129                    byte* pLastElement = pInArray + nrElements;
130                    while (pCurData < pLastElement)
131                        ret += *pCurData++;
132                }
133            }
134            return ret;
135        }
136        #endregion
137 
138        #region public functions
139        /// <summary>
140        /// Concatenate this array
141        /// </summary>
142        /// <param name="A">N-dimensional array. Except for dimensions <paramref name="dim"/>
143        /// the dimensions of A must match the dimensions of this storage</param>
144        /// <param name="dim">Index of dimension to concatenate arrays along.
145        /// If dim is larger than the number of dimensions of any of the arrays,
146        /// its value will be used in modulus the number of dimensions.</param>
147        /// <returns>New array having the size
148        /// of both input arrays layed behind each other along the dim's-dimension</returns>
149        public ILRetLogical Concat(ILInLogical A, int dim) {
150            using (ILScope.Enter(A))
151            return new ILRetLogical ((ILLogicalStorage)Storage.Concat(A.Storage, dim));
152        }
153        /// <summary>
154        /// Create reshaped copy of this logical array
155        /// </summary>
156        /// <param name="size">New dimensions of the array</param>
157        /// <returns>Reshaped copy of this array</returns>
158        /// <remarks><para>The current instance will not be changed! A new array is created, having
159        /// the elements of this array and a shape as determined by <paramref name="size"/>.</para>
160        /// </remarks>
161        /// <exception cref="ILNumerics.Exceptions.ILArgumentException">If the number of elements in
162        /// <paramref name="size"/> do not match the number of elements in this array.</exception>
163        public new ILRetLogical Reshape(ILSize size) {
164            using (ILScope.Enter()) {
165                ILLogical ret = C;
166                ret.Storage.Reshape(size);
167                return ret;
168            }
169        }
170        /// <summary>
171        /// Create reshaped copy of this logical array
172        /// </summary>
173        /// <param name="size">New dimensions of the array</param>
174        /// <returns>Reshaped copy of the array</returns>
175        /// <remarks><para>The current instance will not be changed! A new array is created, having
176        /// the elements of this array and a shape as determined by <paramref name="size"/>.</para>
177        /// </remarks>
178        /// <exception cref="ILNumerics.Exceptions.ILArgumentException">If the number of elements in
179        /// <paramref name="size"/> do not match the number of elements in this array.</exception>
180        public new ILRetLogical Reshape(params int[] size) {
181            return Reshape(new ILSize(size));
182        }
183        /// <summary>
184        /// Create replication of this array
185        /// </summary>
186        /// <param name="dims">Dimensions specifier. If the number of elements in <paramref name="dims"/> is
187        /// less than the number of dimensions in this array, the trailing dimensions will
188        /// be set to 1 (singleton dimensions). On the other hand, if the number specified
189        /// is larger then the number of dimension stored inside the storge the resulting
190        /// storage will get its number of dimensions extended accordingly. </param>
191        /// <returns>array being created out of multiple replications of this array along
192        /// arbitrary dimensions according to <paramref name="dims"/></returns>
193        public new ILRetLogical Repmat(params int[] dims) {
194            return new ILRetLogical((ILLogicalStorage)Storage.Repmat(dims));
195        }
196        /// <summary>
197        /// Create logical array from this logical and shift dimensions
198        /// </summary>
199        /// <param name="shift">Number of dimensions to shift</param>
200        /// <returns>Shifted version of this array</returns>
201        /// <remarks><para>The shift is done 'to the left':</para>
202        /// <example><code>ILArray&lt;double> A = zeros(2,4);
203        /// ILArray&lt;double> B = A.Shifted(1);
204        /// // B is now: &lt;double> [4,2]
205        ///
206        /// ILArray&lt;double> C = zeros(2,4,3);
207        /// ILArray&lt;double> D = C.Shifted(1);
208        /// // D is now: &lt;double> [4,3,2]
209        /// </code></example>
210        /// <para>The dimensions are shifted circulary to the left. This
211        /// can be imagined as removing the first dimensions from the beginning of the list of
212        /// dimensions and "append" them to the end in a ringbuffer style.</para>
213        /// <para>For dimension shifts of '1', you may consider using the
214        /// <see cref="ILNumerics.ILDenseArray{ElementType}.T"/> property for readability.</para>
215        /// <para><paramref name="shift"/> must be positive. It is taken modulus the number of dimensions.</para>
216        /// <seealso cref="ILNumerics.ILDenseArray{ElementType}.T"/></remarks>
217        public new ILRetLogical Shifted(int shift) {
218            return new ILRetLogical((ILLogicalStorage)Storage.ShiftDimensions(shift));
219        }
220        /// <summary>
221        /// Subarray from this array
222        /// </summary>
223        /// <param name="range">Arrays specifying the ranges to create subarray from</param>
224        /// <returns>Subarray as specified</returns>
225        public new ILRetLogical Subarray(params ILBaseArray[] range) {
226            using (ILScope.Enter(range))
227            return new ILRetLogical((ILLogicalStorage)Storage.Subarray(range));
228        }
229        /// <summary>
230        /// Short summary of this logical array
231        /// </summary>
232        /// <returns>Type and size information</returns>
233        public override string ShortInfo() {
234            string ret = "Logical ";
235            if (object.Equals(m_storage,null))
236                return ret + " (disposed)";
237            if (Storage.Size.NumberOfElements == 1)
238                ret += Storage.GetValue(0);
239            else
240                ret += Storage.Size.ToString();
241            return ret.ToString();
242        }
243        #endregion
244
245        #region depricated
246        /// <summary>
247        /// [deprecated] create empty ILLogical
248        /// </summary>
249        /// <returns>empty ILLogical.</returns>
250        [Obsolete()]
251        public static ILRetLogical empty(ILSize dim) {
252            return new ILRetLogical(new byte[0],dim);
253        }
254        #endregion
255
256    }
257}
Note: See TracBrowser for help on using the repository browser.