<div dir="ltr"><div>(Full disclosure: I work on Numba...) <br></div><div><br></div>Just to note, the NumPy implementation will allocate (and free) more than 2 arrays to compute that expression.  It has to allocate the result array for each operation as Python executes.  That expression is equivalent to:<div><br></div>s1 = newfactor * x<br>s2 = np.exp(s1)<br>s3 = np.sin(x)<br>y = s3 * s2<div><br></div><div>However, memory allocation is still pretty fast compared to special math functions (exp and sin), which dominate that calculation.  I find this expression takes around 20 milliseconds for a million elements on my older laptop, so that might be negligible in your program execution time unless you need to recreate this decaying exponential thousands of times.  Tools like Numba or numexpr will be useful to fuse loops so you only do one allocation, but they aren't necessary unless this becomes the bottleneck in your code.</div><div><br></div><div>If you are getting started with NumPy, I would suggest not worrying about these issues too much, and focus on making good use of arrays, NumPy array functions, and array expressions in your code.  If you have to write for loops (if there is no good way to do the operation with existing NumPy functions), I would reach for something like Numba, and if you want to speed up complex array expressions, both Numba and Numexpr will do a good job.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 23, 2019 at 10:38 AM Hameer Abbasi <<a href="mailto:einstein.edison@gmail.com">einstein.edison@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div>
<div>
<div>
<div>
<div style="direction:ltr">Hi Ram,</div>
<div><br>
</div>
<div style="direction:ltr">No, NumPy doesn’t have a way. And it newer versions, it probably won’t create two arrays if all the dtypes match, it’ll do some magic to re use the existing ones, although it will use multiple loops instead of just one.</div>
<div><br>
</div>
<div style="direction:ltr">You might want to look into NumExpr or Numba if you want an efficient implementation.</div>
</div>
<div><br>
</div>
<div class="gmail-m_-6538713471541823960ms-outlook-ios-signature">Get <a href="https://aka.ms/o0ukef" target="_blank">Outlook for iOS</a></div>
</div>
<div> </div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_-6538713471541823960divRplyFwdMsg" dir="dir="ltr""><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> NumPy-Discussion <numpy-discussion-bounces+einstein.edison=<a href="mailto:gmail.com@python.org" target="_blank">gmail.com@python.org</a>> on behalf of Ram Rachum <<a href="mailto:ram@rachum.com" target="_blank">ram@rachum.com</a>><br>
<b>Sent:</b> Tuesday, July 23, 2019 7:29 pm<br>
<b>To:</b> <a href="mailto:numpy-discussion@python.org" target="_blank">numpy-discussion@python.org</a><br>
<b>Subject:</b> [Numpy-discussion] Creating a sine wave with exponential decay
<div> </div>
</font></div>

<div dir="ltr">
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
Hi everyone! Total <span class="gmail-m_-6538713471541823960gmail-il">Numpy</span> newbie here.</p>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
I'd like to create an array with a million numbers, that has a sine wave with exponential decay on the amplitude.</p>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
In other words, I want the value of each cell <code style="margin:0px;padding:1px 5px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-size:13px;vertical-align:baseline;box-sizing:inherit;background-color:rgb(239,240,241);white-space:pre-wrap">n</code> to
 be <code style="margin:0px;padding:1px 5px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-size:13px;vertical-align:baseline;box-sizing:inherit;background-color:rgb(239,240,241);white-space:pre-wrap">sin(n)
 * 2 ** (-n * factor)</code>.</p>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
What would be the most efficient way to do that?</p>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
Someone suggested I do something like this: </p>
<pre class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-lang-py gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-prettyprint gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-prettyprinted" style="white-space:pre-wrap;margin-top:0px;margin-bottom:1em;padding:12px 8px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;font-size:13px;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;background-color:rgb(239,240,241);border-radius:3px;color:rgb(57,51,24)"><code style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,sans-serif;vertical-align:baseline;box-sizing:inherit;white-space:inherit"><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">y </span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">=</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)"> np</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">.</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">sin</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">(</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">x</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">)</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)"> </span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">*</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)"> np</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">.</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">exp</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">(</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">newfactor </span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">*</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pln" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)"> x</span><span class="gmail-m_-6538713471541823960gmail-m_3550683175654527263gmail-pun" style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;color:rgb(48,51,54)">)</span></code></pre>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
But this would create 2 arrays, wouldn't it? Isn't that wasteful? Does <span class="gmail-m_-6538713471541823960gmail-il">Numpy</span> provide an efficient way of doing that without creating a redundant array?</p>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
<br>
</p>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
Thanks for your help,</p>
<p style="color:rgb(36,39,41);font-family:inherit;font-size:15px;font-style:inherit;font-variant:inherit;font-weight:inherit;margin:0px 0px 1em;padding:0px;border:0px;font-stretch:inherit;line-height:inherit;vertical-align:baseline;box-sizing:inherit;clear:both">
Ram Rachum.</p>
</div>
</div>
</div>

_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>