/Assets/ThirdPartyAssets/mathnet-numerics-master/src/Numerics/LinearAlgebra/CreateVector.cs

https://github.com/matryx/calcflow · C# · 312 lines · 148 code · 28 blank · 136 comment · 0 complexity · 2bada20e53b2a64afa13d61d2e5564fe MD5 · raw file

  1. // <copyright file="CreateVector.cs" company="Math.NET">
  2. // Math.NET Numerics, part of the Math.NET Project
  3. // http://numerics.mathdotnet.com
  4. // http://github.com/mathnet/mathnet-numerics
  5. //
  6. // Copyright (c) 2009-2015 Math.NET
  7. //
  8. // Permission is hereby granted, free of charge, to any person
  9. // obtaining a copy of this software and associated documentation
  10. // files (the "Software"), to deal in the Software without
  11. // restriction, including without limitation the rights to use,
  12. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the
  14. // Software is furnished to do so, subject to the following
  15. // conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. // OTHER DEALINGS IN THE SOFTWARE.
  28. // </copyright>
  29. using System;
  30. using System.Collections.Generic;
  31. using MathNet.Numerics.Distributions;
  32. using MathNet.Numerics.LinearAlgebra.Storage;
  33. namespace MathNet.Numerics.LinearAlgebra
  34. {
  35. public static class CreateVector
  36. {
  37. /// <summary>
  38. /// Create a new vector straight from an initialized matrix storage instance.
  39. /// If you have an instance of a discrete storage type instead, use their direct methods instead.
  40. /// </summary>
  41. public static Vector<T> WithStorage<T>(VectorStorage<T> storage)
  42. where T : struct, IEquatable<T>, IFormattable
  43. {
  44. return Vector<T>.Build.OfStorage(storage);
  45. }
  46. /// <summary>
  47. /// Create a new vector with the same kind of the provided example.
  48. /// </summary>
  49. public static Vector<T> SameAs<T,TU>(Vector<TU> example, int length)
  50. where T : struct, IEquatable<T>, IFormattable
  51. where TU : struct, IEquatable<TU>, IFormattable
  52. {
  53. return Vector<T>.Build.SameAs(example, length);
  54. }
  55. /// <summary>
  56. /// Create a new vector with the same kind and dimension of the provided example.
  57. /// </summary>
  58. public static Vector<T> SameAs<T,TU>(Vector<TU> example)
  59. where T : struct, IEquatable<T>, IFormattable
  60. where TU : struct, IEquatable<TU>, IFormattable
  61. {
  62. return Vector<T>.Build.SameAs(example);
  63. }
  64. /// <summary>
  65. /// Create a new vector with the same kind of the provided example.
  66. /// </summary>
  67. public static Vector<T> SameAs<T,TU>(Matrix<TU> example, int length)
  68. where T : struct, IEquatable<T>, IFormattable
  69. where TU : struct, IEquatable<TU>, IFormattable
  70. {
  71. return Vector<T>.Build.SameAs(example, length);
  72. }
  73. /// <summary>
  74. /// Create a new vector with a type that can represent and is closest to both provided samples.
  75. /// </summary>
  76. public static Vector<T> SameAs<T>(Vector<T> example, Vector<T> otherExample, int length)
  77. where T : struct, IEquatable<T>, IFormattable
  78. {
  79. return Vector<T>.Build.SameAs(example, otherExample, length);
  80. }
  81. /// <summary>
  82. /// Create a new vector with a type that can represent and is closest to both provided samples and the dimensions of example.
  83. /// </summary>
  84. public static Vector<T> SameAs<T>(Vector<T> example, Vector<T> otherExample)
  85. where T : struct, IEquatable<T>, IFormattable
  86. {
  87. return Vector<T>.Build.SameAs(example, otherExample);
  88. }
  89. /// <summary>
  90. /// Create a new vector with a type that can represent and is closest to both provided samples.
  91. /// </summary>
  92. public static Vector<T> SameAs<T>(Matrix<T> matrix, Vector<T> vector, int length)
  93. where T : struct, IEquatable<T>, IFormattable
  94. {
  95. return Vector<T>.Build.SameAs(matrix, vector, length);
  96. }
  97. /// <summary>
  98. /// Create a new dense vector with values sampled from the provided random distribution.
  99. /// </summary>
  100. public static Vector<T> Random<T>(int length, IContinuousDistribution distribution)
  101. where T : struct, IEquatable<T>, IFormattable
  102. {
  103. return Vector<T>.Build.Random(length, distribution);
  104. }
  105. /// <summary>
  106. /// Create a new dense vector with values sampled from the standard distribution with a system random source.
  107. /// </summary>
  108. public static Vector<T> Random<T>(int length)
  109. where T : struct, IEquatable<T>, IFormattable
  110. {
  111. return Vector<T>.Build.Random(length);
  112. }
  113. /// <summary>
  114. /// Create a new dense vector with values sampled from the standard distribution with a system random source.
  115. /// </summary>
  116. public static Vector<T> Random<T>(int length, int seed)
  117. where T : struct, IEquatable<T>, IFormattable
  118. {
  119. return Vector<T>.Build.Random(length, seed);
  120. }
  121. /// <summary>
  122. /// Create a new dense vector straight from an initialized vector storage instance.
  123. /// The storage is used directly without copying.
  124. /// Intended for advanced scenarios where you're working directly with
  125. /// storage for performance or interop reasons.
  126. /// </summary>
  127. public static Vector<T> Dense<T>(DenseVectorStorage<T> storage)
  128. where T : struct, IEquatable<T>, IFormattable
  129. {
  130. return Vector<T>.Build.Dense(storage);
  131. }
  132. /// <summary>
  133. /// Create a dense vector of T with the given size.
  134. /// </summary>
  135. /// <param name="size">The size of the vector.</param>
  136. public static Vector<T> Dense<T>(int size)
  137. where T : struct, IEquatable<T>, IFormattable
  138. {
  139. return Vector<T>.Build.Dense(size);
  140. }
  141. /// <summary>
  142. /// Create a dense vector of T that is directly bound to the specified array.
  143. /// </summary>
  144. public static Vector<T> Dense<T>(T[] array)
  145. where T : struct, IEquatable<T>, IFormattable
  146. {
  147. return Vector<T>.Build.Dense(array);
  148. }
  149. /// <summary>
  150. /// Create a new dense vector and initialize each value using the provided value.
  151. /// </summary>
  152. public static Vector<T> Dense<T>(int length, T value)
  153. where T : struct, IEquatable<T>, IFormattable
  154. {
  155. return Vector<T>.Build.Dense(length, value);
  156. }
  157. /// <summary>
  158. /// Create a new dense vector and initialize each value using the provided init function.
  159. /// </summary>
  160. public static Vector<T> Dense<T>(int length, Func<int, T> init)
  161. where T : struct, IEquatable<T>, IFormattable
  162. {
  163. return Vector<T>.Build.Dense(length, init);
  164. }
  165. /// <summary>
  166. /// Create a new dense vector as a copy of the given other vector.
  167. /// This new vector will be independent from the other vector.
  168. /// A new memory block will be allocated for storing the vector.
  169. /// </summary>
  170. public static Vector<T> DenseOfVector<T>(Vector<T> vector)
  171. where T : struct, IEquatable<T>, IFormattable
  172. {
  173. return Vector<T>.Build.DenseOfVector(vector);
  174. }
  175. /// <summary>
  176. /// Create a new dense vector as a copy of the given array.
  177. /// This new vector will be independent from the array.
  178. /// A new memory block will be allocated for storing the vector.
  179. /// </summary>
  180. public static Vector<T> DenseOfArray<T>(T[] array)
  181. where T : struct, IEquatable<T>, IFormattable
  182. {
  183. return Vector<T>.Build.DenseOfArray(array);
  184. }
  185. /// <summary>
  186. /// Create a new dense vector as a copy of the given enumerable.
  187. /// This new vector will be independent from the enumerable.
  188. /// A new memory block will be allocated for storing the vector.
  189. /// </summary>
  190. public static Vector<T> DenseOfEnumerable<T>(IEnumerable<T> enumerable)
  191. where T : struct, IEquatable<T>, IFormattable
  192. {
  193. return Vector<T>.Build.DenseOfEnumerable(enumerable);
  194. }
  195. /// <summary>
  196. /// Create a new dense vector as a copy of the given indexed enumerable.
  197. /// Keys must be provided at most once, zero is assumed if a key is omitted.
  198. /// This new vector will be independent from the enumerable.
  199. /// A new memory block will be allocated for storing the vector.
  200. /// </summary>
  201. public static Vector<T> DenseOfIndexed<T>(int length, IEnumerable<Tuple<int, T>> enumerable)
  202. where T : struct, IEquatable<T>, IFormattable
  203. {
  204. return Vector<T>.Build.DenseOfIndexed(length, enumerable);
  205. }
  206. /// <summary>
  207. /// Create a new sparse vector straight from an initialized vector storage instance.
  208. /// The storage is used directly without copying.
  209. /// Intended for advanced scenarios where you're working directly with
  210. /// storage for performance or interop reasons.
  211. /// </summary>
  212. public static Vector<T> Sparse<T>(SparseVectorStorage<T> storage)
  213. where T : struct, IEquatable<T>, IFormattable
  214. {
  215. return Vector<T>.Build.Sparse(storage);
  216. }
  217. /// <summary>
  218. /// Create a sparse vector of T with the given size.
  219. /// </summary>
  220. /// <param name="size">The size of the vector.</param>
  221. public static Vector<T> Sparse<T>(int size)
  222. where T : struct, IEquatable<T>, IFormattable
  223. {
  224. return Vector<T>.Build.Sparse(size);
  225. }
  226. /// <summary>
  227. /// Create a new sparse vector and initialize each value using the provided value.
  228. /// </summary>
  229. public static Vector<T> Sparse<T>(int length, T value)
  230. where T : struct, IEquatable<T>, IFormattable
  231. {
  232. return Vector<T>.Build.Sparse(length, value);
  233. }
  234. /// <summary>
  235. /// Create a new sparse vector and initialize each value using the provided init function.
  236. /// </summary>
  237. public static Vector<T> Sparse<T>(int length, Func<int, T> init)
  238. where T : struct, IEquatable<T>, IFormattable
  239. {
  240. return Vector<T>.Build.Sparse(length, init);
  241. }
  242. /// <summary>
  243. /// Create a new sparse vector as a copy of the given other vector.
  244. /// This new vector will be independent from the other vector.
  245. /// A new memory block will be allocated for storing the vector.
  246. /// </summary>
  247. public static Vector<T> SparseOfVector<T>(Vector<T> vector)
  248. where T : struct, IEquatable<T>, IFormattable
  249. {
  250. return Vector<T>.Build.SparseOfVector(vector);
  251. }
  252. /// <summary>
  253. /// Create a new sparse vector as a copy of the given array.
  254. /// This new vector will be independent from the array.
  255. /// A new memory block will be allocated for storing the vector.
  256. /// </summary>
  257. public static Vector<T> SparseOfArray<T>(T[] array)
  258. where T : struct, IEquatable<T>, IFormattable
  259. {
  260. return Vector<T>.Build.SparseOfArray(array);
  261. }
  262. /// <summary>
  263. /// Create a new sparse vector as a copy of the given enumerable.
  264. /// This new vector will be independent from the enumerable.
  265. /// A new memory block will be allocated for storing the vector.
  266. /// </summary>
  267. public static Vector<T> SparseOfEnumerable<T>(IEnumerable<T> enumerable)
  268. where T : struct, IEquatable<T>, IFormattable
  269. {
  270. return Vector<T>.Build.SparseOfEnumerable(enumerable);
  271. }
  272. /// <summary>
  273. /// Create a new sparse vector as a copy of the given indexed enumerable.
  274. /// Keys must be provided at most once, zero is assumed if a key is omitted.
  275. /// This new vector will be independent from the enumerable.
  276. /// A new memory block will be allocated for storing the vector.
  277. /// </summary>
  278. public static Vector<T> SparseOfIndexed<T>(int length, IEnumerable<Tuple<int, T>> enumerable)
  279. where T : struct, IEquatable<T>, IFormattable
  280. {
  281. return Vector<T>.Build.SparseOfIndexed(length, enumerable);
  282. }
  283. }
  284. }