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 | /// Create new array, fill elements with constant value
|
---|
55 | /// </summary>
|
---|
56 | /// <typeparam name="T">Element type</typeparam>
|
---|
57 | /// <param name="value">Constant value for all elements</param>
|
---|
58 | /// <param name="size">Size of new array</param>
|
---|
59 | /// <returns>New array according to size with all elements set to 'value'</returns>
|
---|
60 | public static ILRetArray<T> array<T>(T value, ILSize size) {
|
---|
61 | T[] newData = ILMemoryPool.Pool.New<T>(size.NumberOfElements);
|
---|
62 | int i = 0, itemLength, itemCount, workerCount = 1;
|
---|
63 | if (Settings.s_maxNumberThreads > 2 &&
|
---|
64 | size.NumberOfElements >= ILNumerics.Settings.s_minParallelElement1Count / 2) {
|
---|
65 | if (size.NumberOfElements >= ILNumerics.Settings.s_minParallelElement1Count / Settings.s_maxNumberThreads) {
|
---|
66 | itemCount = Settings.s_maxNumberThreads;
|
---|
67 | itemLength = size.NumberOfElements / Settings.s_maxNumberThreads;
|
---|
68 | } else {
|
---|
69 | itemCount = 2;
|
---|
70 | itemLength = size.NumberOfElements / 2;
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | itemCount = 1;
|
---|
74 | itemLength = size.NumberOfElements;
|
---|
75 | }
|
---|
76 | Action<object> worker = (data) => {
|
---|
77 | Tuple<int, int> range = (Tuple<int, int>)data;
|
---|
78 | int start = range.Item1, endEx = range.Item2;
|
---|
79 | for (int c = start; c < endEx; c++) {
|
---|
80 | newData[c] = value;
|
---|
81 | }
|
---|
82 | System.Threading.Interlocked.Decrement(ref workerCount);
|
---|
83 | };
|
---|
84 | for (i = 0; i < workerCount - 1; i++) {
|
---|
85 | System.Threading.Interlocked.Increment(ref workerCount);
|
---|
86 | Tuple<int, int> range = Tuple.Create(i * itemLength, (i + 1) * itemLength);
|
---|
87 | ILThreadPool.QueueUserWorkItem(i, worker, range);
|
---|
88 | }
|
---|
89 | worker(Tuple.Create(i * itemLength, size.NumberOfElements));
|
---|
90 | System.Threading.SpinWait.SpinUntil(() => { return workerCount <= 0; });
|
---|
91 | ILRetArray<T> ret = new ILRetArray<T>(newData, size);
|
---|
92 | return ret;
|
---|
93 | }
|
---|
94 | /// <summary>
|
---|
95 | /// Create new array, fill element with constant value
|
---|
96 | /// </summary>
|
---|
97 | /// <typeparam name="T">Element type</typeparam>
|
---|
98 | /// <param name="value">Constant value for all elements</param>
|
---|
99 | /// <param name="size">Size of new array</param>
|
---|
100 | /// <returns>New array according to size with all elements set to 'value'</returns>
|
---|
101 | public static ILRetArray<T> array<T>(T value, params int[] size) {
|
---|
102 | return array<T>(value, new ILSize(size));
|
---|
103 | }
|
---|
104 | /// <summary>
|
---|
105 | /// Create array, given elements and size
|
---|
106 | /// </summary>
|
---|
107 | /// <typeparam name="T">Element type</typeparam>
|
---|
108 | /// <param name="elements">System.Array of predefined elements</param>
|
---|
109 | /// <param name="size">Size of every dimension for the new array, must correspond to the number of elements in <paramref name="elements"/>.</param>
|
---|
110 | /// <returns>Newly created array</returns>
|
---|
111 | /// <remarks><para>The System.Array given as <paramref name="elements"/>is taken
|
---|
112 | /// as storage for the new array without copy. Make sure not to reference that
|
---|
113 | /// System.Array directly afterwards.</para>
|
---|
114 | /// <para>In order to prevent for memory leaks on long runnning algorithms, <c>System.Array</c>s should
|
---|
115 | /// not get created via the 'new' keyword - but fetched from the ILMemoryPool. </para></remarks>
|
---|
116 | public static ILRetArray<T> array<T>(T[] elements, params int[] size) {
|
---|
117 | ILSize newDimensions = new ILSize(size);
|
---|
118 | ILRetArray<T> ret = new ILRetArray<T>(elements, newDimensions);
|
---|
119 | return ret;
|
---|
120 | }
|
---|
121 | /// <summary>
|
---|
122 | /// Create array, given elements and size
|
---|
123 | /// </summary>
|
---|
124 | /// <typeparam name="T">Element type</typeparam>
|
---|
125 | /// <param name="elements">System.Array of predefined elements</param>
|
---|
126 | /// <param name="size">Size of the new array, must correspond to the number of elements in <paramref name="elements"/>.</param>
|
---|
127 | /// <returns>Newly created array</returns>
|
---|
128 | /// <remarks><para>The System.Array given as <paramref name="elements"/>is taken
|
---|
129 | /// as storage for the new array without copy. Make sure not to reference that
|
---|
130 | /// System.Array directly afterwards.</para>
|
---|
131 | /// <para>In order to prevent for memory leaks on long runnning algorithms, <c>System.Array</c>s should
|
---|
132 | /// not get created via the 'new' keyword - but fetched from the ILMemoryPool.
|
---|
133 | /// </para></remarks>
|
---|
134 | public static ILRetArray<T> array<T>(T[] elements, ILSize size) {
|
---|
135 | ILRetArray<T> ret = new ILRetArray<T>(elements, size);
|
---|
136 | return ret;
|
---|
137 | }
|
---|
138 | /// <summary>
|
---|
139 | /// Create array, given elements and size
|
---|
140 | /// </summary>
|
---|
141 | /// <typeparam name="T">Element type</typeparam>
|
---|
142 | /// <param name="elements">Variable argument list with elements</param>
|
---|
143 | /// <param name="size">Size of the new array, must correspond to the number of elements in <paramref name="elements"/>.</param>
|
---|
144 | /// <returns>Newly created array</returns>
|
---|
145 | /// <remarks><para>The elements given as <paramref name="elements"/> are used
|
---|
146 | /// for the new array without copy. For <typeparamref name="T"/> being a reference type, make sure not to reference any
|
---|
147 | /// elements directly afterwards.</para>
|
---|
148 | /// </remarks>
|
---|
149 | public static ILRetArray<T> array<T>(ILSize size, params T[] elements) {
|
---|
150 | ILRetArray<T> ret = new ILRetArray<T>(elements, size);
|
---|
151 | return ret;
|
---|
152 | }
|
---|
153 | /// <summary>
|
---|
154 | /// Create column vector from given elements
|
---|
155 | /// </summary>
|
---|
156 | /// <typeparam name="T">Element type</typeparam>
|
---|
157 | /// <param name="elements">List of elements</param>
|
---|
158 | /// <returns>Newly created vector with elements given</returns>
|
---|
159 | /// <remarks><para>If an System.Array was given as params argument, the array is directly taken
|
---|
160 | /// as storage for the new array without copy. Make sure not to reference the
|
---|
161 | /// System.Array directly afterwards!</para>
|
---|
162 | /// <para>In order to prevent for memory leaks on long runnning algorithms, <c>System.Array</c>s should
|
---|
163 | /// not get created via the 'new' keyword - but fetched from the ILMemoryPool.</para>
|
---|
164 | /// <para>The shape of the vector created is controlled by the setting switch <see cref="ILNumerics.Settings.CreateRowVectorsByDefault"/>.
|
---|
165 | /// This switch defaults to <c>false</c> which will cause the creation of a column vector. </para></remarks>
|
---|
166 | /// <see cref="ILNumerics.ILMath.row{T}(T[])"/>
|
---|
167 | /// <see cref="ILNumerics.ILMath.column{T}(T[])"/>
|
---|
168 | public static ILRetArray<T> array<T>(params T[] elements) {
|
---|
169 | ILSize newDimensions;
|
---|
170 | if (Settings.CreateRowVectorsByDefault) {
|
---|
171 | newDimensions = new ILSize(1, elements.Length);
|
---|
172 | } else {
|
---|
173 | newDimensions = new ILSize(elements.Length, 1);
|
---|
174 | }
|
---|
175 | ILRetArray<T> ret = new ILRetArray<T>(elements, newDimensions);
|
---|
176 | return ret;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /// <summary>
|
---|
180 | /// Create row vector
|
---|
181 | /// </summary>
|
---|
182 | /// <typeparam name="T">Element type</typeparam>
|
---|
183 | /// <param name="elements">Elements of the row vector</param>
|
---|
184 | /// <returns>New row vector</returns>
|
---|
185 | public static ILRetArray<T> row<T>(params T[] elements) {
|
---|
186 | if (elements == null || elements.Length == 0) {
|
---|
187 | return empty<T>(ILSize.Empty00);
|
---|
188 | }
|
---|
189 | return new ILRetArray<T>(elements, new ILSize(1, elements.Length));
|
---|
190 | }
|
---|
191 | /// <summary>
|
---|
192 | /// Create column vector
|
---|
193 | /// </summary>
|
---|
194 | /// <typeparam name="T">Element type</typeparam>
|
---|
195 | /// <param name="elements">Elements of the column vector</param>
|
---|
196 | /// <returns>New column vector</returns>
|
---|
197 | public static ILRetArray<T> column<T>(params T[] elements) {
|
---|
198 | if (elements == null || elements.Length == 0) {
|
---|
199 | return empty<T>(ILSize.Empty00);
|
---|
200 | }
|
---|
201 | return new ILRetArray<T>(elements, new ILSize(elements.Length, 1));
|
---|
202 | }
|
---|
203 |
|
---|
204 | }
|
---|
205 | }
|
---|