# Tag Info

## Hot answers tagged attributes

35

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 ...

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 ...

29

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 ...

18

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 := SystemDumpAutoLoad[ Hold[Plot], Hold[syms], VisualizationProto ...

17

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}

17

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 ...

16

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 =...

16

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 ...

16

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}

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 ...

13

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

13

I am betting that this is almost certainly an optimization short-cut. If Orderless had to try every ordering it would be extremely slow when there are a moderate number of arguments, but it is not. Consider for example: f @@@ Hold @@ {RandomSample[Range@12]} Hold @@ {f @@ Range@12 /. {7 -> _}} MatchQ[%%, %] Hold[f[2, 6, 11, 7, 12, 10, 4, 1, 3, 5, 8, ...

12

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. ...

11

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

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.

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 ...

9

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 ...

9

This is more like Dynamic: Clear[f] SetAttributes[f, HoldFirst] (*f[x_]:=g[x]*) Table[f[i], {i, 5}] (* {f[i], f[i], f[i], f[i], f[i]} *) By giving f a definition, it evaluated while in Table, during which the i got evaluated.

9

General explanation The semantics of PatternTest and Condition is that the main evaluator is called to test the pattern, when those are used. It is called internally by the pattern-matcher, and this call is independent from the main evaluation procedure for the main expression. The reason is that the evaluations used to determine the pattern match should be ...

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

The main difference can been seen when dealing with list of lists. Consider the following list: lis = {{1, 2, 3}, {3, 4, 5}, {5, 6, 7}}; Lets create a Listable function g SetAttributes[g, Listable] Now we Map a non-listable function f and apply the listable function g Map[f, lis] (* {f[{1, 2, 3}], f[{3, 4, 5}], f[{5, 6, 7}]} *) g[lis] (* {{g[1], g[...

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

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], Tr[4]}...

7

Here is how I make sense of this behavior. When a function that appears in a pattern has attribute Orderless, the pattern-matcher must generate all possible permutations of its argument sequence before trying to match these patterns. Refer to a simple example expression such as a /. b -> c: in a nutshell, as Fred mentioned in his comment below, I ...

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