csharpfftfsharpintegrationinterpolationlinear-algebramathdifferentiationmatrixnumericsrandomregressionstatisticsmathnet
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
302 lines
29 KiB
302 lines
29 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Generating Data
|
|
</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
<meta name="author" content="Christoph Ruegg, Marcus Cuda, Jurgen Van Gael">
|
|
|
|
<link rel="stylesheet" id="theme_link" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/4.6.0/materia/bootstrap.min.css">
|
|
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
|
|
|
|
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"></script>
|
|
|
|
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
|
|
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/navbar-fixed-left.css" />
|
|
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/fsdocs-default.css" />
|
|
<link type="text/css" rel="stylesheet" href="https://numerics.mathdotnet.com/content/fsdocs-custom.css" />
|
|
<script type="text/javascript" src="https://numerics.mathdotnet.com/content/fsdocs-tips.js"></script>
|
|
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
|
|
<!--[if lt IE 9]>
|
|
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
|
|
<!-- END SEARCH BOX: this adds support for the search box -->
|
|
|
|
</head>
|
|
|
|
<body>
|
|
<nav class="navbar navbar-expand-md navbar-light bg-secondary fixed-left" id="fsdocs-nav">
|
|
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse navbar-nav-scroll" id="navbarsExampleDefault">
|
|
<a href="https://numerics.mathdotnet.com/"><img id="fsdocs-logo" src="/logo.png" /></a>
|
|
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
|
|
<div id="header">
|
|
<div class="searchbox" id="fsdocs-searchbox">
|
|
<label for="search-by">
|
|
<i class="fas fa-search"></i>
|
|
</label>
|
|
<input data-search-input="" id="search-by" type="search" placeholder="Search..." />
|
|
<span data-search-clear="">
|
|
<i class="fas fa-times"></i>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<!-- END SEARCH BOX: this adds support for the search box -->
|
|
<ul class="navbar-nav">
|
|
<li class="nav-header">Math.NET Numerics</li>
|
|
<li class="nav-item"><a class="nav-link" href="Packages.html">NuGet & Binaries</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="ReleaseNotes.html">Release Notes</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://github.com/mathnet/mathnet-numerics/blob/master/LICENSE.md">MIT License</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Compatibility.html">Platform Support</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://numerics.mathdotnet.com/api/">Class Reference</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://github.com/mathnet/mathnet-numerics/issues">Issues & Bugs</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Users.html">Who is using Math.NET?</a></li>
|
|
|
|
<li class="nav-header">Contributing</li>
|
|
<li class="nav-item"><a class="nav-link" href="Contributors.html">Contributors</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Contributing.html">Contributing</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Build.html">Build & Tools</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://github.com/mathnet/mathnet-numerics/discussions/categories/ideas">Your Ideas</a></li>
|
|
|
|
<li class="nav-header">Getting Help</li>
|
|
<li class="nav-item"><a class="nav-link" href="https://discuss.mathdotnet.com/c/numerics">Discuss</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="https://stackoverflow.com/questions/tagged/mathdotnet">Stack Overflow</a></li>
|
|
|
|
<li class="nav-header">Getting Started</li>
|
|
<l class="nav-item"i><a class="nav-link" href="/">Getting started</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Constants.html">Constants</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Matrix.html">Matrices and Vectors</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Euclid.html">Euclid & Number Theory</a></li>
|
|
<li class="nav-item">Combinatorics</li>
|
|
|
|
<li class="nav-header">Evaluation</li>
|
|
<li class="nav-item"><a class="nav-link" href="Functions.html">Special Functions</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Integration.html">Integration</a></li>
|
|
|
|
<li class="nav-header">Statistics/Probability</li>
|
|
<li class="nav-item"><a class="nav-link" href="DescriptiveStatistics.html">Descriptive Statistics</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Probability.html">Probability Distributions</a></li>
|
|
|
|
<li class="nav-header">Generation</li>
|
|
<li class="nav-item"><a class="nav-link" href="Generate.html">Generating Data</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="Random.html">Random Numbers</a></li>
|
|
|
|
<li class="nav-header">Solving Equations</li>
|
|
<li class="nav-item"><a class="nav-link" href="LinearEquations.html">Linear Equation Systems</a></li>
|
|
|
|
<li class="nav-header">Optimization</li>
|
|
<li class="nav-item"><a class="nav-link" href="Distance.html">Distance Metrics</a></li>
|
|
|
|
<li class="nav-header">Curve Fitting</li>
|
|
<li class="nav-item"><a class="nav-link" href="Regression.html">Regression</a></li>
|
|
|
|
<li class="nav-header">Native Providers</li>
|
|
<li class="nav-item"><a class="nav-link" href="MKL.html">Intel MKL</a></li>
|
|
|
|
<li class="nav-header">Working Together</li>
|
|
<li class="nav-item"><a class="nav-link" href="CSV.html">Delimited Text Files (CSV)</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="MatrixMarket.html">NIST MatrixMarket</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="MatlabFiles.html">MATLAB</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="IFSharpNotebook.html">IF# Notebook</a></li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
<div class="container">
|
|
<div class="masthead">
|
|
<h3 class="muted">
|
|
<a href="https://numerics.mathdotnet.com">Math.NET Numerics</a> |
|
|
<a href="https://www.mathdotnet.com">Math.NET Project</a> |
|
|
<a href="https://github.com/mathnet/mathnet-numerics">GitHub</a>
|
|
</h3>
|
|
</div>
|
|
<hr />
|
|
<div class="container" id="fsdocs-content">
|
|
<h1><a name="Generating-Data" class="anchor" href="#Generating-Data">Generating Data</a></h1>
|
|
<p>Numerics is all about analyzing and manipulating numeric data. But unless you can read in data from an external
|
|
file, source or e.g. with the excellent <a href="https://fsharp.github.io/FSharp.Data/">F# Type Providers</a>,
|
|
you may need to generate synthetic or random data locally, or transform existing data into a new form.
|
|
The <code>Generate</code> class can help you in all these scenarios with a set of static functions generating either
|
|
an array or an IEnumerable sequence.</p>
|
|
<p>There is some overlap with LINQ, in case of F# also with some integrated language features and its fundamental types.
|
|
This is intended for simplicity and consistency between array and sequence operations, as LINQ only supports sequences.</p>
|
|
<h2><a name="Linear-Range" class="anchor" href="#Linear-Range">Linear Range</a></h2>
|
|
<p>Generates a linearly spaced array within the inclusive interval between start and stop,
|
|
and either a provided step or a step of 1.0. Linear range is equivalent to the
|
|
single colon <code>:</code> and double colon <code>::</code> operators in MATLAB.</p>
|
|
<p>F# has built in linear range support in array comprehensions with the colon operator:</p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="pn">[</span> <span class="o">10.0</span> <span class="o">..</span> <span class="n">2.0</span> <span class="o">..</span> <span class="n">15.0</span> <span class="pn">]</span>
|
|
<span class="fsi">val it : float list = [10.0; 12.0; 14.0] </span>
|
|
<span class="pn">[</span> <span class="k">for</span> <span onmouseout="hideTip(event, 'fs1', 1)" onmouseover="showTip(event, 'fs1', 1)" class="fn">x</span> <span class="k">in</span> <span class="o">10.0</span> <span class="o">..</span> <span class="n">2.0</span> <span class="o">..</span> <span class="n">15.0</span> <span class="k">-></span> <span onmouseout="hideTip(event, 'fs2', 2)" onmouseover="showTip(event, 'fs2', 2)" class="fn">sin</span> <span onmouseout="hideTip(event, 'fs1', 3)" onmouseover="showTip(event, 'fs1', 3)" class="fn">x</span> <span class="pn">]</span>
|
|
<span class="fsi">val it : float list = [-0.5440211109; -0.536572918; 0.9906073557] </span>
|
|
</code></pre>
|
|
<p>In C# you can get the same result with <code>LinearRange</code>:</p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">Generate.LinearRange(<span class="n">10</span>, <span class="n">2</span>, <span class="n">15</span>); <span class="c">// returns array { 10.0, 12.0, 14.0 }</span>
|
|
Generate.LinearRangeMap(<span class="n">10</span>, <span class="n">2</span>, <span class="n">15</span>, Math.Sin); <span class="c">// applies sin(x) to each value</span>
|
|
</code></pre></td></tr></table>
|
|
<p>Most of the routines in the <code>Generate</code> class have variants with a <code>Map</code> suffix.
|
|
Instead of returning an array with the generated numbers, these routines instead
|
|
apply the generated numbers to a custom function and return an array with the results.
|
|
Similarly, some routines have variants with a <code>Sequence</code> suffix that return
|
|
lazy enumerable sequences instead of arrays.</p>
|
|
<h2><a name="Linear-Spaced-and-Log-Spaced" class="anchor" href="#Linear-Spaced-and-Log-Spaced">Linear-Spaced and Log-Spaced</a></h2>
|
|
<p>Generates a linearly or log-spaced array within an interval, but other than linear range
|
|
where the step is provided, here we instead provide the number of values we want.
|
|
This is equivalent to the linspace and logspace operators in MATLAB.</p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">Generate.LinearSpaced(<span class="n">11</span>, <span class="n">0.0</span>, <span class="n">1.0</span>); <span class="c">// returns array { 0.0, 0.1, 0.2, .., 1.0 }</span>
|
|
Generate.LinearSpacedMap(<span class="n">15</span>, <span class="n">0.0</span>, Math.Pi, Math.Sin); <span class="c">// applies sin(x) to each value</span>
|
|
</code></pre></td></tr></table>
|
|
<p>In F# you can also use:</p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="id">Generate</span><span class="pn">.</span><span class="id">linearSpacedMap</span> <span class="n">15</span> <span class="n">0.0</span> <span class="id">Math</span><span class="pn">.</span><span class="id">PI</span> <span onmouseout="hideTip(event, 'fs2', 4)" onmouseover="showTip(event, 'fs2', 4)" class="id">sin</span>
|
|
<span class="fsi">val it : float [] = </span>
|
|
<span class="fsi"> [|0.0; 0.222520934; 0.4338837391; 0.6234898019; 0.7818314825; 0.9009688679; </span>
|
|
<span class="fsi"> 0.9749279122; 1.0; 0.9749279122; 0.9009688679; 0.7818314825; 0.6234898019; </span>
|
|
<span class="fsi"> 0.4338837391; 0.222520934; 1.224606354e-16|] </span>
|
|
</code></pre>
|
|
<p><code>LogSpaced</code> works the same way but instead of the values <span class="math">\(10^x\)</span> it spaces the decade exponents <span class="math">\(x\)</span> linearly
|
|
between the provided two exponents.</p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">Generate.LogSpaced(<span class="n">4</span>,<span class="n">0</span>,<span class="n">3</span>); <span class="c">// returns array { 1, 10, 100, 1000 }</span>
|
|
</code></pre></td></tr></table>
|
|
<h2><a name="Kronecker-Delta-Impulse" class="anchor" href="#Kronecker-Delta-Impulse">Kronecker Delta Impulse</a></h2>
|
|
<p>The Kronecker delta <span class="math">\(\delta[n]\)</span> is a fundamental signal in time-discrete signal processing,
|
|
often referred to as <em>unit impulse</em>. When applied to a system, the resulting output is the system's <em>impulse response</em>.
|
|
It is closely related to the Dirac delta impulse function <span class="math">\(\delta(x)\)</span> in continuous signal processing.</p>
|
|
<p><span class="math">\[\delta[n] = \begin{cases} 0 &\mbox{if } n \ne 0 \\ 1 & \mbox{if } n = 0\end{cases}\]</span></p>
|
|
<p>The <code>Impulse</code> routine generates a Kronecker delta impulse, but also accepts a sample delay
|
|
parameter <span class="math">\(d\)</span> and amplitude <span class="math">\(A\)</span> such that the resulting generated signal is</p>
|
|
<p><span class="math">\[s[n] = A\cdot\delta[n-d] = \begin{cases} 0 &\mbox{if } n \ne d \\ A & \mbox{if } n = d\end{cases}\]</span></p>
|
|
<p>There is also a periodic version in <code>PeriodicImpulse</code> which accepts an additional <code>period</code> parameter.</p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="id">Generate</span><span class="pn">.</span><span class="id">Impulse</span><span class="pn">(</span><span class="n">8</span><span class="pn">,</span> <span class="n">2.0</span><span class="pn">,</span> <span class="n">3</span><span class="pn">)</span>
|
|
<span class="fsi">val it : float [] = [|0.0; 0.0; 0.0; 2.0; 0.0; 0.0; 0.0; 0.0|] </span>
|
|
|
|
<span class="id">Generate</span><span class="pn">.</span><span class="id">PeriodicImpulse</span><span class="pn">(</span><span class="n">8</span><span class="pn">,</span> <span class="n">3</span><span class="pn">,</span> <span class="n">10.0</span><span class="pn">,</span> <span class="n">1</span><span class="pn">)</span>
|
|
<span class="fsi">val it : float [] = [|0.0; 10.0; 0.0; 0.0; 10.0; 0.0; 0.0; 10.0|] </span>
|
|
</code></pre>
|
|
<h2><a name="Heaviside-Step" class="anchor" href="#Heaviside-Step">Heaviside Step</a></h2>
|
|
<p>Another fundamental signal in signal processing, the Heaviside step function <span class="math">\(H[n]\)</span>
|
|
is the integral of the Dirac delta impulse and represents a signal that switches on
|
|
at a specified time and then stays on indefinitely. In discrete time:</p>
|
|
<p><span class="math">\[H[n] = \begin{cases} 0 &\mbox{if } n < 0 \\ 1 & \mbox{if } n \ge 0\end{cases}\]</span></p>
|
|
<p>The <code>Step</code> routines generates a Heaviside step, but just like the Kronecker Delta impulse
|
|
also accepts a sample delay parameter <span class="math">\(d\)</span> and amplitude <span class="math">\(A\)</span> such that the resulting generated signal is</p>
|
|
<p><span class="math">\[s[n] = A\cdot H[n-d] = \begin{cases} 0 &\mbox{if } n < d \\ A & \mbox{if } n \ge d\end{cases}\]</span></p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="id">Generate</span><span class="pn">.</span><span class="id">Step</span><span class="pn">(</span><span class="n">8</span><span class="pn">,</span> <span class="n">2.0</span><span class="pn">,</span> <span class="n">3</span><span class="pn">)</span>
|
|
<span class="fsi">val it : float [] = [|0.0; 0.0; 0.0; 2.0; 2.0; 2.0; 2.0; 2.0|] </span>
|
|
</code></pre>
|
|
<h2><a name="Periodic-Sawtooth" class="anchor" href="#Periodic-Sawtooth">Periodic Sawtooth</a></h2>
|
|
<p>Generates an array of the given length with a periodic upper forward sawtooth signal,
|
|
i.e. a line starting at zero up to some amplitude <span class="math">\(A\)</span>, then drop back to zero instantly and start afresh.
|
|
The sawtooth can be used to turn any arbitrary function defined over the interval <span class="math">\([0,A)\)</span> into a
|
|
periodic function by repeating it continuously.</p>
|
|
<p>Mathematically, the sawtooth can be described using the fractional part function
|
|
<span class="math">\(\mathrm{frac}(x) \equiv x - \lfloor x \rfloor\)</span>, frequency <span class="math">\(\nu\)</span> and phase <span class="math">\(\theta\)</span> as</p>
|
|
<p><span class="math">\[s(x) = A\cdot\mathrm{frac}\left(x\nu+\frac{\theta}{A}\right)\]</span></p>
|
|
<p>In a trigonometric interpretation the signal represents the angular position <span class="math">\(\alpha\)</span> of a point moving endlessly
|
|
around a circle with radius <span class="math">\(\frac{A}{2\pi}\)</span> (and thus circumference <span class="math">\(A\)</span>) in constant speed,
|
|
normalized to strictly <span class="math">\(0\le\alpha < A\)</span>.</p>
|
|
<p><code>Generate.Periodic(length,samplingRate,frequency,amplitude,phase,delay)</code></p>
|
|
<ul>
|
|
<li><strong>Sampling Rate</strong>: Number of samples <span class="math">\(r\)</span> per time unit. If the time unit is 1s, the sampling rate has unit Hz.</li>
|
|
<li>
|
|
<strong>Frequency</strong>: Frequency <span class="math">\(\nu\)</span> of the signal, in sawtooth-periods per time unit. If the time unit is 1s, the frequency has unit Hz.
|
|
For a desired number of samples <span class="math">\(n\)</span> per sawtooth-period and sampling rate <span class="math">\(r\)</span> choose <span class="math">\(\nu=\frac{r}{n}\)</span>.
|
|
</li>
|
|
<li>
|
|
<strong>Amplitude</strong>: The theoretical maximum value <span class="math">\(A\)</span>, which is never reached and logically equivalent to zero.
|
|
The circumference of the circle. Typically <span class="math">\(1\)</span> or <span class="math">\(2\pi\)</span>.
|
|
</li>
|
|
<li><strong>Phase</strong>: Optional initial value or phase offset. Contributes to <span class="math">\(\theta\)</span>.</li>
|
|
<li><strong>Delay</strong>: Optional initial delay, in samples. Contributes to <span class="math">\(\theta\)</span>.</li>
|
|
</ul>
|
|
<p>The equivalent map function accepts a custom map lambda as second argument after the length:</p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="id">Generate</span><span class="pn">.</span><span class="id">periodicMap</span> <span class="n">15</span> <span class="pn">(</span><span class="pn">(</span><span class="o">+</span><span class="pn">)</span> <span class="n">100.0</span><span class="pn">)</span> <span class="n">1000.0</span> <span class="n">100.0</span> <span class="n">10.0</span> <span class="n">0.0</span> <span class="n">0</span>
|
|
<span class="fsi">val it : float [] = </span>
|
|
<span class="fsi"> [|100.0; 101.0; 102.0; 103.0; 104.0; 105.0; 106.0; 107.0; 108.0; 109.0; </span>
|
|
<span class="fsi"> 100.0; 101.0; 102.0; 103.0; 104.0|] </span>
|
|
</code></pre>
|
|
<h2><a name="Sinusoidal" class="anchor" href="#Sinusoidal">Sinusoidal</a></h2>
|
|
<p>Generates a Sine wave array of the given length. This is equivalent to applying a scaled
|
|
trigonometric Sine function to a periodic sawtooth of amplitude <span class="math">\(2\pi\)</span>.</p>
|
|
<p><span class="math">\[s(x) = A\cdot\sin(2\pi\nu x + \theta)\]</span></p>
|
|
<p><code>Generate.Sinusoidal(length,samplingRate,frequency,amplitude,mean,phase,delay)</code></p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">Generate.Sinusoidal(<span class="n">15</span>, <span class="n">1000.0</span>, <span class="n">100.0</span>, <span class="n">10.0</span>);
|
|
<span class="c">// returns array { 0, 5.9, 9.5, 9.5, 5.9, 0, -5.9, ... }</span>
|
|
</code></pre></td></tr></table>
|
|
<h2><a name="Random" class="anchor" href="#Random">Random</a></h2>
|
|
<p>Generate random sequences by sampling from a random distribution.</p>
|
|
<h4><a name="Uniform-Distribution" class="anchor" href="#Uniform-Distribution">Uniform Distribution</a></h4>
|
|
<p>Generate sample sequences distributed uniformly between 0 and 1.</p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">Generate.Uniform(<span class="n">100</span>); <span class="c">// e.g. 0.867421787170424, 0.236744313145403, ... </span>
|
|
</code></pre></td></tr></table>
|
|
<p>Uniform supports mapping to functions with not only one but also two uniform variables
|
|
as arguments, with <code>UniformMap</code> and <code>UniformMap2</code>. As usual, lazy sequences can be
|
|
generated using the variants with the <code>Sequence</code> suffix, e.g. <code>UniformMap2Sequence</code>.</p>
|
|
<h4><a name="Non-Uniform-Distributions" class="anchor" href="#Non-Uniform-Distributions">Non-Uniform Distributions</a></h4>
|
|
<p>Instead of uniform we can also sample from other distributions.</p>
|
|
<ul>
|
|
<li><code>Normal</code> - sample an array or sequence form a normal distribution</li>
|
|
<li><code>Standard</code> - sample an array or sequence form a standard distribution</li>
|
|
</ul>
|
|
<p>In addition, the <code>Random</code> functions accept a custom distribution instance to sample
|
|
from. See the section about random numbers and probability distributions for details.</p>
|
|
<h2><a name="Map" class="anchor" href="#Map">Map</a></h2>
|
|
<p>Generates a new array or sequence where each new values is the result of applying the provided function
|
|
the the corresponding value in the input data.</p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp"><span class="k">var</span> a <span class="o">=</span> <span class="k">new</span> <span class="k">double</span>[] { <span class="n">2.0</span>, <span class="n">4.0</span>, <span class="n">3.0</span>, <span class="n">6.0</span> };
|
|
Generate.Map(a, x <span class="o">=</span><span class="o">></span> x <span class="o">+</span> <span class="n">1.0</span>); <span class="c">// returns array { 3.0, 5.0, 4.0, 7.0 }</span>
|
|
</code></pre></td></tr></table>
|
|
<p>In F# you'd typically use the Array module to the same effect (and should continue to do so):</p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span onmouseout="hideTip(event, 'fs3', 5)" onmouseover="showTip(event, 'fs3', 5)" class="m">Array</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs4', 6)" onmouseover="showTip(event, 'fs4', 6)" class="id">map</span> <span class="pn">(</span><span class="pn">(</span><span class="o">+</span><span class="pn">)</span> <span class="n">1.0</span><span class="pn">)</span> <span onmouseout="hideTip(event, 'fs5', 7)" onmouseover="showTip(event, 'fs5', 7)" class="id">a</span>
|
|
<span class="fsi">val it : float [] = [|3.0; 5.0; 4.0; 7.0|] </span>
|
|
</code></pre>
|
|
<p>...but no equivalent operation is available in the .NET base class libraries (BCL) for C#.
|
|
You can use LINQ, but that operates on sequences instead of arrays:</p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">a.Select(x <span class="o">=</span><span class="o">></span> x <span class="o">+</span> <span class="n">1.0</span>).ToArray();
|
|
</code></pre></td></tr></table>
|
|
<p>Similarly, with <code>Map2</code> you can also map a function accepting two inputs to two input arrays:</p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span class="k">let</span> <span onmouseout="hideTip(event, 'fs6', 8)" onmouseover="showTip(event, 'fs6', 8)" class="id">b</span> <span class="o">=</span> <span class="pn">[|</span> <span class="n">1.0</span><span class="pn">;</span> <span class="o">-</span><span class="n">1.0</span><span class="pn">;</span> <span class="n">2.0</span><span class="pn">;</span> <span class="o">-</span><span class="n">2.0</span> <span class="pn">|]</span>
|
|
<span class="id">Generate</span><span class="pn">.</span><span class="id">Map2</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs5', 9)" onmouseover="showTip(event, 'fs5', 9)" class="id">a</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs6', 10)" onmouseover="showTip(event, 'fs6', 10)" class="id">b</span><span class="pn">,</span> <span class="k">fun</span> <span class="id">x</span> <span class="id">y</span> <span class="k">-></span> <span class="id">x</span> <span class="o">+</span> <span class="id">y</span><span class="pn">)</span>
|
|
<span class="fsi">val it : float [] = [|3.0; 3.0; 5.0; 4.0|] </span>
|
|
</code></pre>
|
|
<p>Typical F# equivalent:</p>
|
|
<pre class="fssnip highlighted"><code lang="fsharp"><span onmouseout="hideTip(event, 'fs3', 11)" onmouseover="showTip(event, 'fs3', 11)" class="m">Array</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs7', 12)" onmouseover="showTip(event, 'fs7', 12)" class="id">map2</span> <span class="pn">(</span><span class="o">+</span><span class="pn">)</span> <span onmouseout="hideTip(event, 'fs5', 13)" onmouseover="showTip(event, 'fs5', 13)" class="id">a</span> <span onmouseout="hideTip(event, 'fs6', 14)" onmouseover="showTip(event, 'fs6', 14)" class="id">b</span>
|
|
<span class="fsi">val it : float [] = [|3.0; 3.0; 5.0; 4.0|] </span>
|
|
</code></pre>
|
|
<p>And in C# with LINQ:</p>
|
|
<table class="pre"><tr><td class="snippet"><pre class="fssnip highlighted"><code lang="csharp">a.Zip(b, (x, y) <span class="o">=</span><span class="o">></span> x <span class="o">+</span> y).ToArray();
|
|
</code></pre></td></tr></table>
|
|
|
|
<div class="fsdocs-tip" id="fs1">val x : float</div>
|
|
<div class="fsdocs-tip" id="fs2">val sin : value:'T -> 'T (requires member Sin)<br /><em><summary>Sine of the given number</summary><br /><param name="value">The input value.</param><br /><returns>The sine of the input.</returns></em></div>
|
|
<div class="fsdocs-tip" id="fs3">Modul Array
|
|
|
|
aus Microsoft.FSharp.Collections<br /><em><summary>Contains operations for working with arrays.</summary><br /><remarks>
|
|
See also <a href="https://docs.microsoft.com/dotnet/fsharp/language-reference/arrays">F# Language Guide - Arrays</a>.
|
|
</remarks></em></div>
|
|
<div class="fsdocs-tip" id="fs4">val map : mapping:('T -> 'U) -> array:'T [] -> 'U []<br /><em><summary>Builds a new array whose elements are the results of applying the given function
|
|
to each of the elements of the array.</summary><br /><param name="mapping">The function to transform elements of the array.</param><br /><param name="array">The input array.</param><br /><returns>The array of transformed elements.</returns><br /><exception cref="T:System.ArgumentNullException">Thrown when the input array is null.</exception></em></div>
|
|
<div class="fsdocs-tip" id="fs5">val a : float []</div>
|
|
<div class="fsdocs-tip" id="fs6">val b : float []</div>
|
|
<div class="fsdocs-tip" id="fs7">val map2 : mapping:('T1 -> 'T2 -> 'U) -> array1:'T1 [] -> array2:'T2 [] -> 'U []<br /><em><summary>Builds a new collection whose elements are the results of applying the given function
|
|
to the corresponding elements of the two collections pairwise. The two input
|
|
arrays must have the same lengths, otherwise an <c>ArgumentException</c> is
|
|
raised.</summary><br /><param name="mapping">The function to transform the pairs of the input elements.</param><br /><param name="array1">The first input array.</param><br /><param name="array2">The second input array.</param><br /><exception cref="T:System.ArgumentException">Thrown when the input arrays differ in length.</exception><br /><exception cref="T:System.ArgumentNullException">Thrown when either of the input arrays is null.</exception><br /><returns>The array of transformed elements.</returns></em></div>
|
|
|
|
</div>
|
|
<!-- BEGIN SEARCH BOX: this adds support for the search box -->
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.css" />
|
|
<script type="text/javascript">var fsdocs_search_baseurl = 'https://numerics.mathdotnet.com/';</script>
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/2.3.8/lunr.min.js"></script>
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/JavaScript-autoComplete/1.0.4/auto-complete.min.js"></script>
|
|
<script type="text/javascript" src="https://numerics.mathdotnet.com/content/fsdocs-search.js"></script>
|
|
<!-- END SEARCH BOX: this adds support for the search box -->
|
|
</div>
|
|
</body>
|
|
|
|
</html>
|
|
|