<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The ILNumerics Blog &#187; Linq</title>
	<atom:link href="https://ilnumerics.net/blog/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>https://ilnumerics.net/blog</link>
	<description>The Productivity Machine  &#124;  A fresh attempt for scientific computing  &#124;  http://ilnumerics.net</description>
	<lastBuildDate>Thu, 05 Dec 2024 09:09:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>ILNumerics and LINQ</title>
		<link>https://ilnumerics.net/blog/ilnumerics-and-linq/</link>
		<comments>https://ilnumerics.net/blog/ilnumerics-and-linq/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 17:43:35 +0000</pubDate>
		<dc:creator><![CDATA[haymo]]></dc:creator>
				<category><![CDATA[Usage]]></category>
		<category><![CDATA[column major]]></category>
		<category><![CDATA[enumerable]]></category>
		<category><![CDATA[ILNumerics]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://ilnumerics.net/blog/?p=113</guid>
		<description><![CDATA[<p>As you may have noticed, ILNumerics arrays implement the IEnumerable interface. This makes them compatible with &#8216;foreach&#8217; loops and all the nice features of LINQ! Consider the following example: (Dont forget to include &#8216;using System.Linq&#8217; and derive your class from ILNumerics.ILMath!) Some people like the Extensions syntax more: I personally find both equivalently expressive. Considerations &#8230; <a href="https://ilnumerics.net/blog/ilnumerics-and-linq/" class="more-link">Continue reading <span class="screen-reader-text">ILNumerics and LINQ</span> <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/ilnumerics-and-linq/">ILNumerics and LINQ</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>As you may have noticed, ILNumerics arrays implement the IEnumerable interface. This makes them compatible with &#8216;foreach&#8217; loops and all the nice features of <a href="http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b" target="_blank">LINQ</a>! </p>
<p>Consider the following example: (Dont forget to include &#8216;using System.Linq&#8217; and derive your class from ILNumerics.ILMath!)</p>
<pre class="brush: csharp; title: ; notranslate">
ILArray&lt;double&gt; A = vec(0, 10);
Console.WriteLine(String.Join(Environment.NewLine, A));

Console.WriteLine(&quot;Evens from A:&quot;); 
var evens = from a in A where a % 2 == 0 select a; 
Console.WriteLine(String.Join(Environment.NewLine, evens));
Console.ReadKey(); 
return; 
</pre>
<p>Some people like the Extensions syntax more: </p>
<pre class="brush: csharp; title: ; notranslate"> 
var evens = A.Where(a =&gt; a % 2 == 0);
</pre>
<p>I personally find both equivalently expressive. </p>
<h2>Considerations for IEnumerable&lt;T&gt; on ILArray&lt;T&gt;</h2>
<p>No option exist in IEnumerable&lt;T&gt; to specify a dimensionality. Therefore, and since ILNumerics arrays store their elements in column major order, enumerating an ILNumerics array will be done along the first dimension. Therefore, when used on a matrix, the enumerator runs along the columns: </p>
<pre class="brush: csharp; title: ; notranslate">
ILArray&lt;double&gt; A = counter(3,4); 
Console.WriteLine(A + Environment.NewLine); 
            
Console.WriteLine(&quot;IEnumerable:&quot;); 
foreach(var a in A) 
    Console.WriteLine(a);
</pre>
<p>&#8230; will give the following:</p>
<pre class="brush: csharp; title: ; notranslate">
&lt;Double&gt; [3,4]
         1          4          7         10
         2          5          8         11
         3          6          9         12

IEnumerable:
1
2
3
4
5
6
7
8
9
10
11
12
</pre>
<p>Secondly, as is well known, accessing elements returned from IEnumerable&lt;T&gt; is only possible in a read-only manner! In order to alter elements of ILNumerics arrays, one should use the explicit API provided by our arrays. See <a href="http://ilnumerics.net/Support_Documentation_Arrays$ArrayAlter.html">SetValue</a>, <a href="http://ilnumerics.net/Support_Documentation_Arrays$ArrayAlter.html">SetRange</a>, <a href="http://ilnumerics.net/Support_Documentation_Arrays$ArrayAlter.html">A[..]</a> = .. and <a href="http://ilnumerics.net/Support_Documentation_Arrays$ArrayImExport.html">GetArrayForWrite()</a> </p>
<p>Lastly, performance considerations arise by excessive utilization of IEnumerable&lt;T&gt; in such situations, where high performance computations are desirable. ILNumerics does integrate well with IEnumerable&lt;T&gt; &#8211; but how well IEnumerable&lt;T&gt; does integrate into the memory management of ILNumerics should be investigated with help of your favorite profiler. I would suspect, most every day scenarios do work out pretty good with LINQ since it concatenates all expressions and queries and iterates the ILNumerics array only once. However, let us know your experiences! </p>
<p>The post <a rel="nofollow" href="https://ilnumerics.net/blog/ilnumerics-and-linq/">ILNumerics and LINQ</a> appeared first on <a rel="nofollow" href="https://ilnumerics.net/blog">The ILNumerics Blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://ilnumerics.net/blog/ilnumerics-and-linq/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
