Tag Info

Hot answers tagged

34

For this purpose, I wrote a small Symbol Information Palette. This palette let's you quickly look up usages, options and attributes of symbols and was tested on Mac OSX and Linux. Installation The source code is hosted on my GitHub site but to preview or install the palette you only have to evaluate this: Get["http://goo.gl/QPywk"] The link is just ...


30

Your question really is about how to make attributes of f affect also the evaluation of other groups of elements, like y and z in f[x___][y___][z___]. To my knowledge, you can not do it other than using tricks like returning a pure function and the like. This is because, the only tool you have to intercept the stages of evaluation sequence when y and z are ...


20

Here is the simplest answer: sum[n_] := Sum[i x[i], {i, 1, n}] x /: D[x[i_], x[j_], NonConstants -> {x}] := KroneckerDelta[i, j] D[sum[n], x[2], NonConstants -> x] $\begin{cases} 2 & n>1 \\ 1-n & \text{True} \end{cases}$ The trick here is the use of the NonConstants option of the derivative operator. This then has to be ...


15

Yes, this form exists, and was first shown to me by Leonid. It is: Function[Null, (* body with ## *), (* attributes *)] As always the Null may be implicit, so in your application: Function[, Length[Unevaluated@#1]{##2}, HoldFirst][1+2,2+3,3+1] {10, 8}


15

The best option then is to bestow the attribute NHold on the head f. In[2]:= SetAttributes[f, NHoldAll] In[3]:= 2 + f[Sqrt[2] + 1] // N Out[3]= 2. + f[1 + Sqrt[2]]


15

I think the documentation needs to be more clear on this; the order of definitions is important: Remove[plus] Attributes[plus] = {Orderless}; plus[x__Integer, y__Real] := x + y plus[2.5, 3] 5.5 So the Orderless attribute must be active at the time the definition is created. Noteworthy is that definitions made before setting the attribute can ...


14

Generally, a good advice would be to set attributes before giving definitions to the function. The difference in your case is caused by the effect that I call evaluation during assignments. There was a question devoted to it in the past, where I contributed an answer with a detailed analysis of the problem. I also mentioned this problem in my recent ...


14

Ok, I'm going to try to explain my best conjuecture as to how this happens, and don't even try to answer why. There are three reasons for this behaviour: SetDelayed left hand side evaluation As others have mentioned, even though SetDelayed has attributes that indicate it holds the lhs, it does evaluate the head and the arguments of it, just not the ...


13

It is because, in version 9, the implementation of Plot is loaded from a dump file on its first usage, rather than loading when the kernel starts. One can see this by clearing the ReadProtected attribute: ClearAttributes[Plot, ReadProtected] Information[Plot] (* -> Plot := System`Dump`AutoLoad[ Hold[Plot], Hold[syms], Visualization`Proto` ...


12

If you have not saved the attributes before changing them, and also can't quit the Kernel, then you could launch a Subkernel and get the original attributes that way: ClearAttributes[Log, Listable] Attributes[Log] {NumericFunction, Protected} First@ParallelEvaluate[Attributes[Log]] {Listable,NumericFunction,Protected}


11

Save defaults before any changes attrLog = Log // Attributes; ClearAttributes[Log, Listable] Log // Attributes {NumericFunction, Protected} Restore defaults Attributes[Log] = attrLog {Listable, NumericFunction, Protected}


10

I did some computation of formal derivatives a while back which might be of interest in this context (though keep in mind that this is anything but bullet proof! it does work for the cases I bothered to check though). Clear[a]; Format[a[k_]] = Subscript[a, k] Let us say we have an objective function which is formally a function of the vector a[i] Q ...


10

No, I do not believe it is. As the documentation for your error message says: The attributes available in each version of Mathematica are fixed and cannot be changed. The system attributes are low level properties that fundamentally change the evaluation of symbols. I think it makes sense that these are not mixed with high-level user constructs, even ...


10

My guess would be this: ToExpression[ Names["pack`*"], InputForm, Function[sym,SetAttributes[sym, {ReadProtected(*,Locked*)}],HoldFirst] ] The problem is that functions defined with # - & notation do not hold their arguments.


10

Yes, they are considered pseudo-listable. Often they are implemented with something similar to f[x_, a_List] := f[x, #]& /@ a


9

You could also use Distribute: Integrate[integrand, mp] // Distribute $m^2 \text{mp}+\frac{\text{mp}^3}{3}+\int \text{mp} f[\text{mp}] \, d\text{mp}$


9

Here's an example that should help: In[1]:= makeFoo[] := foo @@ RandomInteger[10, 3] foo[first_, _, _]["First"] := first foo[_, second_, _]["Second"] := second foo[_, _, third_]["Third"] := third foo[a_, b_, c_][x_] := a x^2 + b x + c In[6]:= f = makeFoo[] Out[6]= foo[4, 2, 9] In[7]:= f["Second"] Out[7]= 2 In[8]:= f[x] Out[8]= 9 + 2 x + 4 x^2 For the ...


8

This is the expected behaviour of Unevaluated, but is not fully covered in the documentation. Unevaluated is a special head that changes the attributes temporarily so that the function f holds its argument. It is supposed to work as if you had given it one of the HoldFirst/HoldAll, etc. attribute, but only for that evaluation. The key point though, is that ...


8

When you don't restart the Kernel (by using Quit[] or restarting Mathmeatica) then you will always get this behaviour because (1) you have protected the functions yourself and (2) you try to redefine them by reloading the package. It is like evaluating the following twice: f[x_]:=x^2; Protect[f] During evaluation of SetDelayed::write: Tag f in f[x_] ...


8

GCD[a] returns unevaluated because the definitions of GCD only apply when all arguments are numeric. The presence of even one non-numeric argument yields an unevaluated result: ClearAll[a] GCD[1, 2, 3, a] (* GCD[1, 2, 3, a] *) This is true even when the sole argument is non-numeric: GCD[a] (* GCD[a] *) The attribute OneIdentity has no bearing on this ...


8

ReplaceAll ReplaceAll does not behave as a Listable head. If it did it would be broken. Consider: SetAttributes[brokenReplaceAll, Listable] brokenReplaceAll[{1, 2, 3}, {{2 -> "b"}, {2 -> "X"}}] Thread::tdlen: Objects of unequal length in brokenReplaceAll[{1,2,3},{{2->b},{2->X}}] cannot be combined. >> If it were Listable then arbitrarily ...


7

For your specific problem, the following piece of code will work In[1]:= myFunc[data_] := Module[{result}, result["property"] = "123abc"; result[v_] := data + v*3; Return[result]; ]; In[2]:= a=myFunc[10]; a["property"] a[x] Out[2]= 123abc 10 + 3x


7

It is not very common, but sometimes, $Failed is used as a head, like f[x___]:= $Failed[x] This makes it possible to have "return code" returned, rather than just a fact of failure. Basically, when this is used, it is usually in the error-reporting fall-back rule. In some cases, one may want to not evaluate the arguments x (e.g. if f is Hold*). I don't ...


7

OneIdentity is a poorly, and I think incorrectly, documented attribute. It is not an attribute that would have the effect that f[x]=x, as can be seen in your GCD example. According to the documentation, it is an attribute that can be assigned to a symbol f to indicate that f[x], f[f[x]], etc. are all equivalent to x for the purpose of pattern matching. ...


7

No, there isn't. There are several reasons for that: Tr operates on tensors of arbitrary rank, not just matrices Listable functions will automatically thread to the deepest level of lists, so if you set Tr to be Listable, it'll individually wrap each deepest element of a nested list, e.g. Tr[{{1,2},{3,4}}] would transform to {{Tr[1], Tr[2]}, {Tr[3], ...


6

You have to think about, what happens when you evaluate the line l1={a,b,c}. At this point, all your variables on the right side are evaluated and l1 contains only the data. The solution is simple: use Hold instead of List a = 1; b = 2; c = 3; l1 = Hold[a, b, c]; ReleaseHold[{printName /@ l1}]


6

You're right, applying FullDefinition I see that DiracDelta lacks the NumericFunction attribute. And indeed, NumericQ[DiracComb[1]] yields True whereas NumericQ[DiracDelta[0]] doesn't. Although I'm not sure why that difference exists, you may perhaps be able to get the desired result (you didn't say what your bug was) by setting SetAttributes[DiracDelta, ...


6

This is obviously an annoying problem - you can't easily keep the flexibility to reload the package with Get during the development and at the same time keep certain functions Protected / Locked. Just Protected by itself can be dealt with, as explained by halirutan in his answer, but if you add Locked, you are out of luck. Perhaps, the easiest way out is to ...


6

Leaving aside the wisdom of modifying System functions this is an interesting question. I would not have been surprised to see this behavior had the input list been packed as I have already learned about low-level optimizations on packed arrays. See: Block attributes of Equal However that does not appear to be the issue here since (typed in) {1, 2, 3} ...


5

You can use Map because it works with expressions of Head other than List, too: integrand = m^2 + mp^2 + mp f[mp]; Map[Integrate[#, mp] &, integrand] $\int \text{mp} f(\text{mp}) \, d\text{mp}+m^2 \text{mp}+\frac{\text{mp}^3}{3}$ Or the definitie integral: Map[Integrate[#, {mp, -1, 1}] &, integrand] $\int_{-1}^1 \text{mp} ...



Only top voted, non community-wiki answers of a minimum length are eligible