Highest voted questions tagged operators - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-16T07:24:10Z https://mathematica.stackexchange.com/feeds/tag?tagnames=operators&sort=votes https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/56504 65 V10's Operator Forms - what are they good for? Ronald Monson https://mathematica.stackexchange.com/users/2249 2014-08-03T10:12:43Z 2016-01-02T00:39:31Z <p>V10 introduces an operator form for several functions perhaps primarily due to their role in queries as part of introducing data science functionality. At first pass it seems a lot of effort to add some syntactic sugar (given an equivalent pure functional form only ever requires an extra couple of symbols - (<code>#</code>, <code>&amp;</code>) )? For example,<code>Map[f,#]&amp;[{a,b,c}]</code>can now be shortened to <code>Map[f][{a,b,c}]</code>, - slightly more compact but then again perhaps not such an improvement on an existing operator (short) form - <code>f/@{a,b,c}</code>.</p> <p>So, are there some compelling examples that illustrate the rationale behind the introduction of this new construct?</p> <p><strong>Conclusion</strong></p> <p>To summarize the points made in all the informative responses:</p> <ul> <li>In addition to avoiding the symbols (<code>(#&amp;)</code>) operator forms can eliminate the need for <code>Function</code> in nested definitions.</li> <li>The gains of using operator form are cumulative as they are chained together either in postfix, prefix or for some, infix form.</li> <li>While not necessarily restricted to this area the motivation and applicability of operator forms stems from the need to provide functions as arguments in <code>Dataset</code>.</li> <li>Many operator forms are built-in but when not they can be readily defined.</li> <li>The pure and operator forms are not always semantically equivalent (natively or user-defined) with, for example, <code>Query</code> using their different patterns to interpret differently.</li> <li>They can potentially be used to improve efficiency not just via code's reduced leaf-count but in reduced algorithmic complexity.</li> <li>They are potentially a rich source of language improvement from mimicking natural language patterns, code refactoring, debugging or automated and non-deterministic parsing via corpus-derived context.</li> </ul> <h1>Update V 10.3.1 ( 01/01/16 )</h1> <p>A <a href="https://mathematica.stackexchange.com/a/103160/2249">new answer</a> gives an overview of the idioms used for system operator forms and how these can be intermingled with user-defined operator forms.</p> https://mathematica.stackexchange.com/q/30425 63 when is f@g not the same as f[g]? Nasser https://mathematica.stackexchange.com/users/70 2013-08-14T06:42:57Z 2017-06-23T08:09:18Z <p>I have always thought that <code>f@g</code> will give the same result as <code>f[g]</code> in all cases, and it is just a matter of style which one to use and that <code>g</code> will always evaluates first, and then <code>f</code> will evaluate using the result of <code>g</code> evaluation. I never thought that there can be any precedence issue here, since no one ever mentioned it in all the times I have been using Mathematica.</p> <p>So I was really surprised when I found one case where this was not so. So my question is: How does one know when <code>f@g</code> is not the same as <code>f[g]</code> ? </p> <p>The help says nothing about this (thanks to chat room for giving me the link to this, I searched and could not find it)</p> <p><a href="http://reference.wolfram.com/mathematica/ref/Prefix.html">http://reference.wolfram.com/mathematica/ref/Prefix.html</a></p> <p>Even though one can see the word <code>precedence</code> and <code>grouping</code> but no explanation of where these are talked about and no more links to follow</p> <p><code>Prefix[expr, h, precedence, grouping] can be used to specify how the output form should be parenthesized.</code></p> <p>clearly this is a precedence issue. But I have never seen this mentioned before any where.</p> <pre><code>Tr[Times @@@ {{2, 3}, {4, 5}}] </code></pre> <p><img src="https://i.stack.imgur.com/YaTTM.png" alt="Mathematica graphics"></p> <pre><code>Tr @ Times @@@ {{2, 3}, {4, 5}} </code></pre> <p><img src="https://i.stack.imgur.com/cfoD2.png" alt="Mathematica graphics"></p> <pre><code>Tr @ ( Times @@@ {{2, 3}, {4, 5}} ) </code></pre> <p><img src="https://i.stack.imgur.com/uNYZs.png" alt="Mathematica graphics"></p> <p>What seems to have happened is that in <code>Tr@Times@@@....</code> the command <code>Tr</code> grabbed <code>Times</code> before <code>Times</code> was applied. You can replaced <code>Tr</code> by <code>Total</code> also and see the same effect.</p> <p>ps. This is another reason for me to not use <code>@</code> too much. I I really never liked to use <code>@</code> and always liked the good old fashioned <code>[]</code> as it seems clearer also, and now safer also.</p> <p><strong>question is</strong>: What is the rule(s) of thumb to use? One should always look ahead and check before using <code>@</code> to make sure precedence is met? Any other cases than this one might have to watch out for? If there are very few cases, may be one can add them to their cheat sheet. Where are the <code>precedence</code> of all operators listed so one can check? </p> https://mathematica.stackexchange.com/q/60045 34 Is there a comprehensive list of functions with operator forms? Martin Ender https://mathematica.stackexchange.com/users/2305 2014-09-18T19:44:42Z 2016-09-15T08:47:52Z <p>As of version 10, Mathematica sports operator forms for many functions like <code>Map</code>, <code>SortBy</code>, <code>Select</code> which allow you to curry one or more of the arguments. Like</p> <pre><code>m = Map[myFunction]; </code></pre> <p>which creates a new function <code>m</code> which automatically maps <code>myFunction</code> to any list that's passed to it.</p> <p>As the question title says, is there a comprehensive list of which functions support this now, or do I just have to recheck every function I'm using from now on, in case this feature was added (and is useful in my current problem)?</p> <p><strong>Edit:</strong> I just found <a href="http://reference.wolfram.com/language/guide/FunctionCompositionAndOperatorForms.html" rel="noreferrer">this list</a> buried in the docs, but it's not complete (e.g. <code>GroupBy</code> is missing).</p> https://mathematica.stackexchange.com/q/5030 30 Having the derivative be an operator Andrew Spott https://mathematica.stackexchange.com/users/54 2012-05-02T21:47:35Z 2017-12-25T23:23:17Z <p>How do I create an operator which acts like a derivative to everything to the right of it:</p> <p>for example:</p> <p>$\left( \partial_x + \partial_y + z \right ) x \psi$</p> <p>How do I make that evaluate to:</p> <p>$x \partial_x \psi + \psi + x \partial_y \psi + z x \psi$</p> <p>(I want the derivatives to distribute...)</p> https://mathematica.stackexchange.com/q/31375 25 How is + as an infix operator associated with Plus? MathCraft https://mathematica.stackexchange.com/users/8338 2013-08-29T23:51:13Z 2015-08-02T22:22:54Z <p>How is <code>+</code> associated with <code>Plus</code>? Suppose I want to define a head <code>FooBar</code> and have it associated with a unicode character of my choice, ideally used as an infix form, i.e.</p> <pre><code>FooBar[x, y] </code></pre> <p>is equivalent</p> <pre><code>x ↗ y </code></pre> <p>as seen by the kernel. How this kind of thing can be done?</p> https://mathematica.stackexchange.com/q/10445 23 Generating an ordered list of pairs of elements from ordered lists TheDoctor https://mathematica.stackexchange.com/users/2263 2012-09-11T09:44:33Z 2012-09-13T06:55:12Z <p>I have a pair of ordered lists. I want to generate a new ordered list (using the same ordering) of length n by applying a binary operator to pairs of elements, one from each list, along with the index of each list's elements.</p> <p>For example, if I have two lists of real numbers in increasing order (sorted via Less),</p> <pre><code>l1 = {5.7832, 30.4713, 74.887, 139.04, 222.932, 326.563} l2 = {3.481, 9.2816, 15.7112, 27.8226, 45.0379, 67.222, 94.3526} </code></pre> <p>and the binary operator is Plus, then if I ask for 5 elements, the result would be</p> <pre><code>{{9.26417, 1, 1}, {15.0647, 1, 2}, {21.4944, 1, 3}, {33.6058, 1, 4}, {33.9522, 2, 1}} </code></pre> <p>I've written code that achieves this but I'm looking for the most elegant, efficient, and general solution that would work with arbitrary lists and binary operators.</p> https://mathematica.stackexchange.com/q/191373 20 What is the definition of Curl in Mathematica? H. R. https://mathematica.stackexchange.com/users/34873 2019-02-12T08:59:46Z 2019-05-17T14:30:34Z <p>I have a usual mathematical background in vector and tensor calculus. I was trying to use the differential operators of Mathematica, namely <code>Grad</code>, <code>Div</code> and <code>Curl</code>. According to my knowledge, the definitions of Mathematica for <code>Grad</code> and <code>Div</code> coincides with those usually employed in tensor calculus, that is to say</p> <p><span class="math-container">\begin{align*} \text{grad}\mathbf{T}&amp;:=\sum_{k=1}^{3}\frac{\partial\mathbf{T}}{\partial x_k}\otimes \mathbf{e}_k\\ \text{div}\mathbf{T}&amp;:=\sum_{k=1}^{3}\frac{\partial\mathbf{T}}{\partial x_k}\cdot\mathbf{e}_k \\ \tag{1} \end{align*}</span></p> <p>for any tensor <span class="math-container">$\mathbf{T}$</span> of rank <span class="math-container">$n\ge1$</span>. <span class="math-container">$x_k$</span>'s are Cartesian coordinates and <span class="math-container">$\mathbf{e}_i$</span>'s are the standard basis for <span class="math-container">$\mathbb{R}^3$</span>. <span class="math-container">$\otimes$</span> and <span class="math-container">$\cdot$</span> are the usual generalized outer and inner products which are also defined in Mathematica by <code>Outer</code> and <code>Inner</code>. The usual definition that I know from tensor calculus for the <code>Curl</code> is as follows <span class="math-container">\begin{align*} \text{curl}\mathbf{T}&amp;:=\sum_{k=1}^{3}\mathbf{e}_k\times\frac{\partial\mathbf{T}}{\partial x_k}. \tag{2} \end{align*}</span> However, it turns out that Mathematica's definition for curl is totally different. For example, it returns the <code>Curl</code> of a second order tensor as a scalar, while according to <span class="math-container">$(2)$</span> it should be a second order tensor.</p> <blockquote> <p>I couldn't find a precise definition of Mathematica for <code>Curl</code> in the documents. I am wondering what this definition is. What is the motivation for this? and How it can be related to the definition given in <span class="math-container">$(2)$</span>?</p> </blockquote> <p>Below is a simple piece of code for you to observe the outputs of Mathematica when we apply the <code>Grad</code>, <code>Div</code> and <code>Curl</code> operators to scalar, vector and second order tensor fields. I would like to draw your attention to some observations. <code>Curl</code> of a scalar is returned as a second order tensor, which I don't understand why! <code>Curl</code> of a vector coincides with the usual definition of <code>Curl</code> used in vector calculus. <code>Curl</code> of second order tensor is returned as a scalar, which I don't understand again.</p> <pre><code>Var={Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]}; Sca=\[Phi][Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]]; Vec={Subscript[v, 1][Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]],Subscript[v, 2][Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]],Subscript[v, 3][Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]]}; Ten=Table[Subscript[T, i,j][Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]],{i,1,3},{j,1,3}]; MatrixForm[Grad[Sca, Var]] MatrixForm[Grad[Vec, Var]] MatrixForm[Grad[Ten, Var]] MatrixForm[Div[Sca, Var]] MatrixForm[Div[Vec, Var]] MatrixForm[Div[Ten, Var]] MatrixForm[Curl[Sca, Var]] MatrixForm[Curl[Vec, Var]] MatrixForm[Curl[Ten, Var]] </code></pre> <p>I will be happy if someone can reproduce the following result for the curl of a second order tensor with Mathematica's <code>Curl</code> function.</p> <p><span class="math-container">\begin{align*} \text{curl}\mathbf{T}&amp;:=\sum_{k=1}^{3}\mathbf{e}_k\times\frac{\partial\mathbf{T}}{\partial x_k}=\sum_{k=1}^{3}\mathbf{e}_k\times\frac{\partial}{\partial x_k}\left(\sum_{i=1}^{3}\sum_{j=1}^{3}T_{ij}\mathbf{e}_i\otimes\mathbf{e}_j\right)\\ &amp;=\sum_{k=1}^{3}\sum_{i=1}^{3}\sum_{j=1}^{3}\frac{\partial T_{ij}}{\partial x_k}(\mathbf{e}_k\times\mathbf{e}_i)\otimes\mathbf{e}_j\\ &amp;=\sum_{k=1}^{3}\sum_{i=1}^{3}\sum_{j=1}^{3}\sum_{m=1}^{3}\epsilon_{kim}\frac{\partial T_{ij}}{\partial x_k}\mathbf{e}_m\otimes\mathbf{e}_j \tag{3} \end{align*}</span></p> <p>where <span class="math-container">$\epsilon_{kim}$</span> is the <code>LeviCivitaTensor</code> for <span class="math-container">$3$</span> dimensions. Consequently, we get</p> <p><span class="math-container">\begin{align*} \left(\text{curl}\mathbf{T}\right)_{mj}=\sum_{k=1}^{3}\sum_{i=1}^{3}\epsilon_{kim}\frac{\partial T_{ij}}{\partial x_k}. \tag{4} \end{align*}</span></p> <p>Implementing <span class="math-container">$(4)$</span> in Mathematica, we obtain</p> <pre><code>CurlTen = Table[ Sum[ LeviCivitaTensor[[k, i, m]] D[Subscript[T, i, j][Subscript[x, 1], Subscript[x, 2], Subscript[x, 3]], {Subscript[x, k]}], {k, 1, 3}, {i, 1, 3}], {m, 1, 3}, {j, 1, 3}]; MatrixForm[CurlTen] </code></pre> https://mathematica.stackexchange.com/q/71348 17 What happens when you divide by ##? Martin Ender https://mathematica.stackexchange.com/users/2305 2015-01-08T17:51:59Z 2015-08-04T10:19:58Z <p>I've been playing around with sequences a bit. In particular with using <code>##</code> with unary and binary operators.</p> <p>Let's start simple, the following all make some kind of sense:</p> <pre><code> + ## &amp; [a,b] (* a + b *) x + ## &amp; [a,b] (* x + a + b *) x * ## &amp; [a,b] (* x * a * b *) x ^ ## &amp; [a,b] (* x ^ a ^ b *) </code></pre> <p>Now here is a slightly weird case:</p> <pre><code> - ## &amp; [a,b] (* -a*b *) x - ## &amp; [a,b] (* x - a*b *) </code></pre> <p>I guess, this sort of makes sense if <code>-</code> is actually interpreted as something like <code>+(-1)*</code>. But it also means that <code>+##-##</code> is generally non-zero.</p> <p>But now here's the real puzzle:</p> <pre><code>x / ## &amp; [a,b] (* x a^(1/b) *) x / ## &amp; [a,b,c] (* x a^b^(1/c) *) </code></pre> <p>Wh... what? Can anyone explain what's happening here or at least give some justification like the one for subtraction? Answers which correct my explanation for subtraction are also welcome!</p> <p>(No, I would never use this stuff in production code. But knowing what exactly is going on under the hood <a href="https://codegolf.stackexchange.com/search?q=user%3A8478+%5Bcode-golf%5D+mathematica+wiki%3Ano">could come in handy some time</a>.)</p> <p><strong>Bonus Question:</strong> Are there any other operators that yield unexpected and potentially "useful" results? (I mean, <code>!##</code> will yield <code>Not[a,b]</code> but that's neither very unexpected nor useful.)</p> https://mathematica.stackexchange.com/q/5847 15 How to define a non-standard algebra in Mathematica? Anixx https://mathematica.stackexchange.com/users/651 2012-05-21T02:07:42Z 2015-12-10T04:33:59Z <p>I want to define an algebra, where there are three elements: 0, 1 and $\infty$ and two operations, addition and multiplication defined, both commutative:</p> <p>\begin{align*} 0+0&amp;=0\\ 0+1&amp;=1\\ 0+\infty&amp;=\infty\\ 1+1&amp;=1\\ 1+\infty&amp;=\infty\\ \infty+\infty&amp;=\infty\\ 0\times0&amp;=0\\ 1\times0&amp;=0\\ 1\times1&amp;=1\\ 0\times\infty&amp;=1\\ 1\times\infty&amp;=\infty\\ \infty\times\infty&amp;=\infty \end{align*}</p> <p>I want Mathematica to simplify expressions in this system.</p> https://mathematica.stackexchange.com/q/27081 14 Is it possible to define custom compound assignment operators like ⊕= similar to built-ins +=, *= etc? Vladimir Reshetnikov https://mathematica.stackexchange.com/users/7288 2013-06-16T04:51:46Z 2014-08-08T18:42:48Z <p>I would like to do something like this:</p> <pre><code>In:= ToExpression["\"\\[CirclePlus]\""] Out= ⊕ In:= list_ ⊕ element_ := Append[list, element]; In:= a = {1, 2}; In:= {a = a ⊕ 3, a} Out= {{1, 2, 3}, {1, 2, 3}} In:= {a ⊕= 4, a} (* Desired result: Out= {{1, 2, 3, 4}, {1, 2, 3, 4}} *) (* Actual result: syntax error *) Syntax::sntxf: "a⊕" cannot be followed by "=4". </code></pre> <p>Is it possible to workaround this error and make the compound assignment operator <code>⊕=</code> work?</p> <hr> <p>Another example: I would like to create a new infix operator <code>@@@@</code> (or other) to express <code>Apply[f,expr,{2}]</code> in a short form like <code>f @@@@ expr</code>. If precisely this is not possible what are the valid "names" I could use?</p> https://mathematica.stackexchange.com/q/145685 14 How I can define this operator in Mathematica? Masacroso https://mathematica.stackexchange.com/users/44169 2017-05-10T13:14:25Z 2017-12-27T14:01:44Z <p>I want to define an operator $G$ such that</p> <p>$$G(f):=\begin{cases}f(\{x\}),&amp;\lfloor x\rfloor\text{ is even}\\\frac1{f(\{x\})},&amp;\lfloor x\rfloor\text{ is odd}\end{cases}$$</p> <p>for any function $f$, where $\{x\}$ means "fractional part of $x$". I dont have a clue about how to do this. I wanted to write something like</p> <pre><code>G[2 + Sin[x]] </code></pre> <p>that define the above over the function $f(x)=2+\sin x$.</p> https://mathematica.stackexchange.com/q/51514 13 Exponential of a Differential Operator user85503 https://mathematica.stackexchange.com/users/8459 2014-06-25T15:23:08Z 2014-06-26T14:11:04Z <p>In Mathematica, is it possible to exponentiate a differential operator such that the operator will act on a function, $f(x,p)$? Specifically, I wondering if I can get Mathematica to do this:</p> <p>$\exp(c\frac{d}{dp})f(x,p)=(1+c\frac{d}{dp}+\frac{c^2}{2}\frac{d^2}{dp^2}+\cdots)f(x,p)=(f(x,p)+c\frac{df}{dp}+\frac{c^2}{2}\frac{d^2f}{dp^2}+\cdots)$</p> <p>where $c\in\Re$.</p> <p>Thanks.</p> https://mathematica.stackexchange.com/q/112860 13 Custom operator form for system functions? M.R. https://mathematica.stackexchange.com/users/403 2016-04-18T04:43:53Z 2017-01-13T18:42:07Z <p>There are some functions for which an operator form would make sense but isn't implemented. For example, I'd like to make this work:</p> <pre><code>RandomSample @ Range </code></pre> <p>You can do something like this:</p> <pre><code>Unprotect[RandomSample]; Off[RandomSample::lrwl] RandomSample[n_][x_] := RandomSample[x, n] </code></pre> <p>But I wonder if there is a standard technique to define operator forms for system functions?</p> https://mathematica.stackexchange.com/q/95666 12 Element-wise test on List elements dario https://mathematica.stackexchange.com/users/34430 2015-09-28T12:45:04Z 2018-08-16T22:01:38Z <p>This question could sound pretty silly but I can't find a way to apply element -wise tests to a list.</p> <p>For example if I digit</p> <pre><code>{0.6, 1.2}&gt;1 {{0.6,1.2},{5,0.1}}&gt;1 </code></pre> <p>I would expect to obtain</p> <pre><code>{False,True} {{False,True},{True, False}} </code></pre> <p>respectively, but it is not the case.</p> <p>Of course I can define a function or using <code>Map</code>, but I can't believe there is not a core function providing this kind of result. Thank you for any indication</p> https://mathematica.stackexchange.com/q/424 12 How to find range in which a number falls, from given list of numbers? Prashant Bhate https://mathematica.stackexchange.com/users/99 2012-01-21T08:58:30Z 2017-02-05T04:26:13Z <p>How can one find the range in which a number falls, from given list of numbers?</p> <pre><code>f[x_, list_List] := ??? (* Return {a,b} where a &amp; b belongs to list {a,b} forms shortest possible interval which match condition if a&lt;=x&lt;=b {a,b} if x &lt;= a {-∞,a} if x &gt;= b {b,∞} *) </code></pre> <p><code>f</code> Should also consider outer ranges $-\infty$ and $\infty$</p> https://mathematica.stackexchange.com/q/143583 12 What is the best way to define Wirtinger derivatives Aymane Fihadi https://mathematica.stackexchange.com/users/21513 2017-04-14T11:38:41Z 2017-11-14T18:26:49Z <p><a href="https://en.wikipedia.org/wiki/Wirtinger_derivatives" rel="noreferrer">Wirtinger derivatives</a> ( also called Cauchy operators) in complex analysis are widely used tools. They are defined in the case of one dimensional complex plane as follows</p> <p>$$\frac{\partial}{\partial z}=\frac12\left(\frac{\partial}{\partial x}-i\frac{\partial}{\partial y}\right),\quad \frac{\partial}{\partial \bar{z}}=\frac12\left(\frac{\partial}{\partial x}+i\frac{\partial}{\partial y}\right)$$</p> <p>Where $z=x+i y$ and x,y are real variables. Apparently <em>Mathematica</em> does not support directly these operators. For instance, as it is pointed in my question; <a href="https://mathematica.stackexchange.com/questions/143535/defining-a-complex-partial-differential-operator">Defining a complex partial differential operator</a>, <code>D[ , ]</code> don't support a complex argument #2.</p> <p>What is the best way to generalize <code>D</code> such that it supports complex variable <code>z = x + I y</code>, such that it is homogeneous with derivatives of the real part <code>x</code> and imaginary part <code>y</code> in a fashion where results of formal computations can be given in terms of <code>x</code> and <code>y</code>, or in terms of <code>z</code> and <code>Conjugate[z]</code></p> <hr> <h2><strong><em>Update</em> :</strong></h2> <p>Following the suggestion of @xzczd, let me articulate about the concept I have in my head. But the details here are not all requirements for an answer on my question above. It is indeed the converse. As a beginner on Mathematica, any insight can be very helpful for me</p> <p>Let denote by <code>Dc</code> The wanted generalization of <code>D</code>. The first argument of <code>Dc</code> will be a complex function, expressed in term of a variable <code>z := x + I y</code>. The second argument will be <code>x</code> OR <code>y</code> OR <code>z</code> OR <code>Conjugate[z]</code> (in general real or complex). The third argument will contain a rule of the form <code>Coordinates-&gt;"Complex"</code> or <code>"real"</code> which depends of the wanted output whether in terms of $\partial_z$ and $\partial_{\bar{z}}$ or in terms of $\partial_x$ and $\partial_{y}$. note that $\partial_z$ and $\partial_{\bar{z}}$ are defined by the formulas given above. Let's suppose that <code>"Complex"</code> is the default value.</p> <p><strong>Examples:</strong> (I denote by === the output, I use some TeX code, hope it is clear)</p> <p><em>Basic identities</em></p> <pre><code>Dc[z,z] === 1 Dc[Conjugate[z],z] === 0 Dc[Abs[z],Conjugate[z]] === z Dc[x,z] === 1/2 </code></pre> <p><em>General identities</em></p> <pre><code>Dc[f[z],z] === \partial_z[f[z]] Dc[f[z],z,Coordinates-&gt;"Real"] === 1/2 \partial_x[f[z]] - 1/2 I \partial_y[f[z]] x Dc[f[z],x] + y Dc[f[z],y] === z\partial_z[f[z]] + \bar{z} \partial_{\bar{z}} [f[z]] </code></pre> <p>I hope it is more clear now, and the concept makes some sense. Let me know if you need further explanations.</p> https://mathematica.stackexchange.com/q/22856 11 Replace operators Rorschach https://mathematica.stackexchange.com/users/1561 2013-04-07T09:16:41Z 2013-04-07T20:13:03Z <p><code>/.</code> can be used to replace variables but how can one replace operators.</p> <p>For example in <code>a * b</code>, <code>*</code> can be replaced by <code>+</code> or <code>x</code> or <code>/</code> etc. Is there some function to achieve this?</p> https://mathematica.stackexchange.com/q/5434 10 Using D to find a symbolic derivative C. E. https://mathematica.stackexchange.com/users/731 2012-05-11T07:29:45Z 2016-10-30T02:30:01Z <p>I need to do the following:</p> <ol> <li>Define a function</li> <li>Take the derivative of this function and have a look at the symoblic representation</li> <li>Substitute in some values</li> </ol> <p>With the bonus that I want to use the correct variable names everywhere to not mess up my head. So I have defined:</p> <pre><code>a = 0.04; L1 = 1; L0 = 1; </code></pre> <p>My function being</p> <pre><code>L[s_, L0_, L1_, a_] := L1 + L0/(1 + s/a); </code></pre> <p>I don't know how to easily take the derivative of this expression when the arguments are defined (and I don't want to make up new variable names). The best way I could find was using module, like this:</p> <pre><code>Module[{s, L0, L1, a}, D[L[s, L0, L1, a], s]] </code></pre> <p>But this had an unexpected result,</p> <p>$-\frac{\text{L0$\$$7293}}{\text{a\$$7293} \left(1+\frac{\text{s$\$$7293}}{\text{a\$$7293}}\right)^2}$</p> <p>I can't figure out where $7293 comes from? Also, is there a better way, perhaps a way so that I don't have to specify the variables I want to localize? Just telling Mathematica, "do this symbolically".</p> https://mathematica.stackexchange.com/q/63747 10 Operate over list of pure functions rhermans https://mathematica.stackexchange.com/users/10397 2014-10-21T16:27:50Z 2017-08-04T15:16:13Z <p>I have list of pure functions (All functions are <code>InterpolatingFunction</code>) <em>i.e</em> </p> <pre><code>{{a, b}, {c, d}, {e, f}, ...} </code></pre> <p>and I would like to end up with </p> <pre><code>{ (a[#]/b[#])&amp;, (c[#]/d[#])&amp;,(e[#]/f[#])&amp;,...} </code></pre> <p>the closest I have got is to do</p> <pre><code>(Divide @@ Through[#[x]]) &amp; /@ {{a, b}, {c, d}, {e, f}} </code></pre> <blockquote> <pre><code>{a[x]/b[x], c[x]/d[x], e[x]/f[x]} </code></pre> </blockquote> <p>but these are not pure functions.</p> https://mathematica.stackexchange.com/q/16969 10 How do I expand a sum? Misery https://mathematica.stackexchange.com/users/742 2012-12-26T18:04:35Z 2017-08-25T16:40:32Z <p>I have a problem with <em>Mathematica</em>'s symbolic manipulations. As an example, consider the following expression:</p> <p>$$\sum _{i=1}^n -2 x_i \left(-a x_i-b+y_i\right)=0$$</p> <p>How do I get <em>Mathematica</em> to expand it into this form:</p> <p>$$-\sum _{i=1}^n x_i y_i +a \sum _{i=1}^n x_i^2+b \sum _{i=1}^n x_i=0$$</p> <p>What I mean is: what functions do I apply to get the product to expand and the summation operator to distribute?</p> https://mathematica.stackexchange.com/q/95027 10 Question about Rules and Pattern (The operator patt/;test) Sepideh Abadpour https://mathematica.stackexchange.com/users/30689 2015-09-19T18:39:59Z 2015-10-23T12:20:21Z <p>Maybe this question is so trivial but it has confused me.<br> I'm studying the <a href="https://mathematica.stackexchange.com/a/25616/30689">What the @#%^&amp;*?! do all those funny signs mean?</a> and in the <code>Rules and patterns</code> under the <a href="http://reference.wolfram.com/language/ref/Condition.html" rel="nofollow noreferrer">Reference for the operator /;</a> there is this example: </p> <pre><code>In:= f[x_] := ppp[x] /; x &gt; 0 In:= f Out= ppp In:= f[-6] Out= f[-6] </code></pre> <p>Based on the above example I thought the answer of the following code should be <strong>f[-2]</strong>, as the following: </p> <pre><code>In:= f[x_] = Sqrt[x] /; x &gt; 0 Out= Sqrt[x] /; x &gt; 0 In:= f Out= Sqrt /; 2 &gt; 0 In:= f[-2] Out= f[-2] </code></pre> <p>I mean the definition <code>f[x_] = Sqrt[x]</code> should only be used when <code>x&gt;0</code>. So when I enter <code>-2</code> as the argument, the definition should not be used and the output will be <code>f[-2]</code>. But in fact mathematica evaluates the code as follows: </p> <pre><code>In:= f[x_] = Sqrt[x] /; x &gt; 0 Out= Sqrt[x] /; x &gt; 0 In:= f Out= Sqrt /; 2 &gt; 0 In:= f[-2] Out= i Sqrt /; -2 &gt; 0 </code></pre> <p>In spite of the fact that <code>-2&lt;0</code>, <em>Mathematica</em> uses the definition and produces the answer$i\sqrt{2}$<br> What's the difference between this code and the first one that makes mathematica use the definition in spite of the negative argument passed to the function?</p> <hr> <p><em>Update: Response to closure (Michael E2)</em></p> <p>There must be something quite subtle going on. It is not just the usual explanation that <code>Set</code> evaluates the right-hand and <code>SetDelayed</code> does not, because the right-hand side evaluates to itself (assuming, as <a href="https://mathematica.stackexchange.com/users/121/mr-wizard">Mr. Wizard's</a> answer points out, that <code>x</code> has no value). This can be seen because the down values are the same in each case:</p> <pre><code>f1[x_] := Sqrt[x] /; x &gt; 0; f2[x_] = Sqrt[x] /; x &gt; 0; DownValues@f1 DownValues@f2 (* {HoldPattern[f1[x_]] :&gt; Sqrt[x] /; x &gt; 0} *) (* {HoldPattern[f2[x_]] :&gt; Sqrt[x] /; x &gt; 0} *) </code></pre> https://mathematica.stackexchange.com/q/11908 10 Change associativity of the @ operator sakra https://mathematica.stackexchange.com/users/68 2012-10-11T13:19:27Z 2012-10-11T16:46:49Z <p>The @ operator in Mathematica is right-associative by default, i.e.</p> <pre><code>f@g@h </code></pre> <p>evaluates as </p> <pre><code>f@(g@h) </code></pre> <p>Is it possible to make the @ operator evaluate the involved functions in a left-associative order <code>(f@g)@h</code> instead?</p> https://mathematica.stackexchange.com/q/96665 10 Define multilinear operator simon https://mathematica.stackexchange.com/users/23244 2015-10-10T10:44:26Z 2015-10-10T18:53:45Z <p>In <em>Maple</em> it is possible to <a href="http://www.maplesoft.com/support/help/Maple/view.aspx?path=define" rel="noreferrer">define</a> a multilinear, orderless operator with:</p> <pre><code>define(A,'orderless',multilinear): </code></pre> <p>I can't seem to find the equivalent in <em>Mathematica</em>.</p> <p>I would like it to do something like:</p> <pre><code>A[x,x]/.x-&gt;0+b x+c x^2 (* b^2 A[x,x]+2 b c A[x, x^2]+c^2 A[x^2,x^2] *) </code></pre> https://mathematica.stackexchange.com/q/56717 10 Using MapIndexed[List] on a dataset fails -- why? Szabolcs https://mathematica.stackexchange.com/users/12 2014-08-06T00:21:14Z 2014-08-06T14:42:03Z <p>The following works:</p> <pre><code>MapIndexed[List]@{1, 2, 3} (* {{1, {1}}, {2, {2}}, {3, {3}}} *) </code></pre> <p>This works too:</p> <pre><code>Dataset[{1, 2, 3}][MapIndexed[f]] </code></pre> <p><img src="https://i.stack.imgur.com/XbyOa.png" alt="Mathematica graphics"></p> <p>Why does this not work?</p> <pre><code>Dataset[{1, 2, 3}][MapIndexed[List]] </code></pre> <p><img src="https://i.stack.imgur.com/0yjpN.png" alt="Mathematica graphics"></p> <p>This works fine again:</p> <pre><code>Dataset[{1, 2, 3}][MapIndexed[{##} &amp;]] </code></pre> <p><img src="https://i.stack.imgur.com/rSeIb.png" alt="Mathematica graphics"></p> https://mathematica.stackexchange.com/q/84619 9 Defining quantum-mechanical Bra and Ket operations Sid https://mathematica.stackexchange.com/users/26461 2015-05-28T10:33:32Z 2015-05-28T18:54:18Z <p>I have the following ket in the Fock basis:$\vert3, 0 ,1\rangle$, where each entry defines the number of photons in a particular mode and can take any one of the following numbers: 0, 1, 2, 3. As a result I define the following column vectors$0 = (1, 0, 0, 0)^\text{T}$,$1 = (0, 1, 0, 0)^\text{T}$,$2 = (0, 0, 1, 0)^\text{T}$and$3 = (0, 0, 0, 1)^\text{T}$, where the superscript$\text{T}$defines the transpose. </p> <p>I want to be able to define the ket and associate bra terms to evaluate inner products of the form$\langle\alpha, \beta, \gamma \vert\cdot\vert a, b, c\rangle = \delta_{\alpha a}\delta_{\beta b}\delta_{\gamma c}$. Note that the kets$\langle\alpha, \beta, \gamma\vert$is the Hermitian conjugate of the corresponding bra$\vert \alpha, \beta, \gamma\rangle$.</p> <p>How could I implement these inner-products in <em>Mathematica</em>? I imagine that the functions <code>KroneckerProduct</code>, <code>Transpose</code> and the <code>KroneckerDelta</code> would be of use here but I am still unsure. I am aware that there is an add-on for quantum mechanical operations for <em>Mathematica</em> (see <a href="http://library.wolfram.com/infocenter/MathSource/7622/" rel="noreferrer">this page</a>), but I am sure that this problem does not require this. </p> https://mathematica.stackexchange.com/q/135805 9 Which operators are missing from the official precedence table? Martin Ender https://mathematica.stackexchange.com/users/2305 2017-01-20T09:46:25Z 2018-08-23T02:05:17Z <p>I often use the <a href="http://reference.wolfram.com/language/tutorial/OperatorInputForms.html" rel="noreferrer">Operator Input Forms</a> page as a reference for operator precedence as well as which (used or unused) operators exist. However, I just noticed that at least one operator is missing from the table: <code>Divisible</code> can be written as the operator <code>∣</code> (<code>\[Divides]</code>). The immediate problem is that I now need to use trial and error to figure out its precedence, but it raises the more important question which other valid operators (with or without built-in meaning) are missing from the table.</p> <p>Is there a more reliable resource on existing operators, or a way to get a list of all existing operators (ideally with precedence) using meta programming?</p> https://mathematica.stackexchange.com/q/52077 9 Defining the Moyal Product in Mathematica user85503 https://mathematica.stackexchange.com/users/8459 2014-07-04T15:59:09Z 2018-05-01T20:12:29Z <p>What would be the best way to define the Moyal Product,$f(x,p)\star g(x,p)$, in Mathematica?</p> <p>$f(x,p)\star g(x,p)$may be written as$f\star g=\sum\limits_{n=0}^{\infty}\frac{1}{n!}\left(\frac{i\hbar}{2}\right)^n\Pi^n(f,g)$where$\Pi^n=\sum\limits_{k=0}^n(-1)^k{n\choose k}\frac{\partial^nf}{\partial p^k\partial x^{n-k}}\frac{\partial^ng}{\partial x^k\partial p^{n-k}}$</p> <p>I'm also wondering how I would then be able to define$f(x,p)\tilde{\star}g(x,p)$as$f\star(1+\alpha\star_1+\alpha^2\star_2)g$with$\star_1=cp^3\overleftarrow{\partial_p}\overrightarrow{\partial_p}$,$\star_2=c^2p^6\overleftarrow{\partial^2_p}\overrightarrow{\partial^2_p}$, and c being a constant.</p> <p>Thanks.</p> <p>$\Large{\rm{\bf{Edit}}}$</p> <p>Here's how I tried to define the Moyal product:</p> <pre><code>Star[f_, g_] = Sum[(I*h/2)^n* Sum[(-1)^k*Binomial[n, k]*D[f[x, p], {p, k}, {x, n - k}]* D[g[x, p], {x, k}, {p, n - k}], {k, 0, n}], {n, 0, Infinity}] </code></pre> <p>which output</p> <p>$\sum _{n=0}^{\infty } 2^{-n} (i h)^n \sum _{k=0}^n (-1)^k \binom{n}{k} f^{(n-k,k)}(x,p) g^{(k,n-k)}(x,p)$</p> <p>I know that$x\star p=xp+\frac{i\hbar}{2}$, so I tried Star[x,p], but got the following output:</p> <p>$\sum _{n=0}^{\infty } 2^{-n} (i h)^n \sum _{k=0}^n (-1)^k \binom{n}{k} p^{(k,n-k)}(x,p) x^{(n-k,k)}(x,p)$</p> <p>I think my problem is where I used f and g in the function of Star, but I'm not sure how to fix it. Once I figure out how to define$f\star g$in Mathematica, I think I'll be able to define$f\tilde{\star} g$.</p> <p>$\Large{\rm{\bf{Edit}}}$</p> <p>I've included the$\frac{1}{n!}\$ in the definition of the Moyal Product.</p> https://mathematica.stackexchange.com/q/140609 8 Change Operator Precedence of --> operator Neel Basu https://mathematica.stackexchange.com/users/22562 2017-03-21T12:16:17Z 2018-08-10T05:18:13Z <p>I am trying to use <code>--&gt;</code> operator with highest precedence </p> <pre><code>Unprotect[LongRightArrow]; LongRightArrow[obj_,property_]:=obj[ToString[property]]; Protect[LongRightArrow]; </code></pre> <p>With this I can do basic operations like accessing properties of an association</p> <pre><code>In:= obj = &lt;|"a" -&gt; {2, 3}, "b" -&gt; 5|&gt; Out:= &lt;|"a" -&gt; {2, 3}, "b" -&gt; 5|&gt; In:= obj⟶a Out:= {2, 3} </code></pre> <p>However when I try to access elements of list in <code>obj--&gt;a</code> <code>Part</code> takes higher precedence. Same applies for operator <code>^</code>.</p> <pre><code>In:= obj⟶a[] Out:= Missing["KeyAbsent", "a[]"] In:= obj⟶a^2 Out:= Missing["KeyAbsent", "2 a"] </code></pre> https://mathematica.stackexchange.com/q/89706 8 Redefining a built-in operator Omegaman https://mathematica.stackexchange.com/users/31158 2015-08-01T22:46:42Z 2015-08-02T12:17:59Z <p>Is there a way I can redefine the <code>||</code> operator such that <code>a||b</code> will be <code>1/(1/a + 1/b)</code>?</p> <p>Is it possible to define it infix as above and prefix such that <code>||[a,b,c]</code> is <code>1/(1/a+1/b+1/c)</code>?</p> <p>I'm working with circuit impedances and it's a much more natural way of describing the circuit to use <code>+</code> for series and something simple like <code>||</code> for parallel.</p> https://mathematica.stackexchange.com/q/95123 8 Why Drop and Take do not support a operator form Sungmin https://mathematica.stackexchange.com/users/10763 2015-09-21T11:04:14Z 2015-10-07T14:20:37Z <p>Since Mathematica 10 introduced the operator form, I have been a big fan of it. Whenever I use some buit-in functions, I first look up whether they support operator form. </p> <p>To me, it was quite surprising to find that the two seemingly innocent functions <code>Drop</code> and <code>Take</code> do not support operator form as their typical use case requires two arguments and thus making them operator forms, if only one argument is given, does not seem to result in any ambiguity.</p> <p>I later found that they actually accept one argument. Their current behaviour, when only one argument is given, is that they work as <code>Identity</code>. This is probably the reason why they cannot support operator form. What is the underlying reason for this behaviour and is there any chance they will support the operator form in later releases?</p>