# Tag Info

3

Fundamental problem Pardon me if I miss some points of your question as I didn't attempt to understand what your code is intended to do, because I think I understand what the problem is from the title alone. Please consider: SetAttributes[f, {HoldFirst, Listable}]; f[x_] := foo[x] f[{1, 2, 3}] {foo[1], foo[2], foo[3]} bar = {1, 2, 3}; f[bar] ...

10

In V10, another option is to use Association. par=<|"mu"->1,"sigma"->1,"lb"->0,"ub"->10|>; f[x_, p_Association:par] := PDF[LogNormalDistribution[p["mu"], p["sigma"]], x] Plot[f[x, ##], {x, #lb, #ub}] &@par Another form for Plot is: Plot[f[x, par], {x, par@"lb", par@"ub"}] And as @Mr.Wizard commented, you can use the default ...

12

There are a number of options and their attractiveness will depend on the scenario for their use, therefore it is difficult to make any broad recommendations of best practice. I will say that generally it is not recommended to rely on global assignments as in your first example, because this method scales poorly and because it is easy to make mistakes and ...

1

A slightly different way: list = Range @ 49; MapIndexed[(f[First @ #2] = #1)&, list]

4

list = Range[20]^2 (*{1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, \ 289, 324, 361, 400}*) f[n_] := list[[n]] f[10] (*100*)

1

You don't want to pass the full data set into the Manipulate. You just want to pass its name and have it evaluated inside the Manipulate. Try the following. Is it fast enough? SeedRandom[42]; data = RandomReal[{0, 1}, {500, 500, 500}]; SetAttributes[vizData, HoldFirst]; vizData[dataVar_Symbol] := Manipulate[Image[dataVar[[All, All, i]]], {i, 1, 500, 1}] ...

0

I think Bob Hanlon had the right idea in using Orderless but his suggestion is overly naive. Instead we must treat only the triplets as orderless so we will need an additional head. SetAttributes[o, Orderless]; f[a_, b_, c_, x_, y_, z_] := f[o[a, b, c], o[x, y, z]] f[o[0 ..], o[0, y_, z_]] := -(1/(y - z)) Log[y/z] Now all of these match that one rule: ...

-1

Give f the attribute Orderless SetAttributes[f, Orderless]; f[0, 0, 0, 0, z_, z_] = -(1/z); f[0, 0, 0, 0, y_, z_] = -(1/(y - z)) Log[y/z]; f @@@ Permutations[{0, 0, 0, 0, z, z}] // Union {-(1/z)} f @@@ Permutations[{0, 0, 0, 0, y, z}] // Union {-(Log[y/z]/(y - z))}

0

You should define your functions outside of the Manipulate so the definitions aren't reevaluated each time the slider is moved. In doing this, you'll need to make g and U explicitly depend on ρ. g[e_, ρ_] := { {e, ρ, 11}, {1, e, ρ}, {1, 1, e} }; U[e_, ρ_] := Transpose[Eigenvectors[g[e, ρ]]] . { {0, 0, 1}, {0, 1, 0}, {1, 0, 0} }; Next it ...

0

I would suggest: Try to write your function definition outside of the Manipulate, but in dependence of the changing parameter, like so: g[ρ_, e_] := ( {{e, ρ, 11}, {1, e, ρ}, {1, 1, e}} ); U[ρ_, e_] := (Transpose[Eigenvectors[g[ρ, e]]]).( { {0, 0, 1}, {0, , 0}, {1, 0, 0}} ); And than inside the Manipulate use this function definition: Manipulate[ ...

1

Here's an iterated function system that implements Barnesley's fractal fern, which can be generated by four linear systems, chosen randomly with probabilities p1, p2, p3 and p4. a1 = {{0, 0}, {0, 0.16}}; b1 = {0, 0}; p1 = 0.01; a2 = {{0.85, 0.04}, {-0.04, 0.85}}; b2 = {0, 1.6}; p2 = 0.85; a3 = {{0.2, -0.26}, {0.23, 0.22}}; b3 = {0, 1.6}; p3 = 0.07; a4 = ...

3

I find using Module the easiest way to keep track of things when it comes to these kinds of situations. plot[x_, s_] := Module[{b, w, c, ua, ub}, b = 10 x; w = s + b; c = x^2; ua = w - c; ub = w - c^2; Plot[{ua, ub}, {x, -5, 5}]] plot[randomVar, 5]

0

After some contemplation (and some errating comments) I realized that exactly on the real axis above z=1 the function PolyLog(k,z) is not well defined but a choice must be made from which bank of the branch cut we are approaching the real axis (hence my "contemplation" led to a well known result). Mathematica has made that choice which is, however, hidden in ...

6

It comes down to the DRY principle: The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." The content management system Wordpress doesn't use object oriented paradigms and so for that reason it looks exactly like your code. Tens of thousands of lines of code like this. ...

3

Initial problem There is, in my opinion, nothing wrong with "multidependences" in the way I think you mean, but there is a more fundamental problem here (I believe). Consider these definitions: w[b_, x_] := fixed + b[x] u[w_, b_, x_] := Sqrt[w[b, x]] I presume that you expect to call u with three arguments and have it in turn call w but this does not ...

4

Well that's a hairy one. I like it though, as it forced me to think about aspects of evaluation that I am normally oblivious to. Unfortunately that thinking didn't lead to any great insights. My only idea so far is to interrupt evaluation and mess with the Stack as Leonid did for How do you set attributes on SubValues? I have little experience in this ...

0

UPD: Looks like I accidentally dismissed everything in the post below the horizontal line confusing it to comments, and tried to answered a more general question than needed. Too bad. To clarify: the idea represented below is to have [still immutable] objects with pointers (in the form of symbols) to their properties inside the objects themselves, which ...

0

Functional code is often encouraged over procedural code by experienced Mathematica users. For learning purposes, here's such a version: checkIsomorph[start_: {}] := Module[{outList, check}, outList = start; Function[ {seedling}, If[ (check = FreeQ[outList, _?(IsomorphicGraphQ[seedling, #] &)]), AppendTo[outList, seedling] ]; ...

2

You need non-standard evaluation for what you are trying to do. AppendTo needs the name (a symbol) of the list you are trying munge and has the attribute HoldFirst for that reason. For your function to pass that name to AppendTo, it must be given the HoldFirst attribute, too, and you must take care to ensure the unevaluated symbol gets passed to AppendTo. ...

4

How about the old Gayley-Villegas trick? Obj /: (lhs_ = Obj[id_]) := Block[{$inSet = True}, lhs /: (lhs["Property"] = value_) := setObjProperty[id, value]; lhs /: Unset[lhs] := ClearAll[lhs]; lhs = Obj[id] ] /; ! TrueQ[$inSet] Then we get the following behaviour: obj = Obj[1]; UpValues[obj] {HoldPattern[obj["Property"] = value$_] ... 2 In version 10 under Windows I am unable to reproduce the problem you describe: Unprotect[ClearAll]; ClearAll[] := ClearAll["Global*"] SyntaxInformation[ClearAll] = {"ArgumentsPattern" -> {___}}; Protect[ClearAll] In the Notebook: Without the definition above: 3 You can use SyntaxInformation. In this case, SyntaxInformation[Lim] = {"ArgumentsPattern" -> {_, _, OptionsPattern[]}, "LocalVariables" -> {"Limit", {2}}} does what you want. 1 This is an example of how to handle the operation, arguments are your list and the Q: With[{l = #1, q = #2}, QFactorial[Total@l, q]/Times @@ (QFactorial[#, q] & /@ l)] &[{1, 2,3}, 5] (* 79315236 *) 16 I see no mention of the new-in-10 PositionIndex in the other answers, which takes a list (or association) of values and returns a 'reverse lookup' that maps from values in the list to the positions where they occur: In[1]:= index = PositionIndex[{a, b, c, a, c, a}] Out[1]= <|a -> {1, 4, 6}, b -> {2}, c -> {3, 5}|> It doesn't take a level ... 6 A Condition is treated as part of the unique pattern of every assignment, even on the right-hand-side: f := 1 /; foo f := 2 /; bar Definition[f] f := 1 /; foo f := 2 /; bar You are using the notably unusual form: lhs := Module[{vars}, rhs /; test] allows local variables to be shared between test and rhs. You can use the same construction with ... 1 You could also do this : With[{y = x}, SetDelayed @@ {Unevaluated[draw[x_]], Unevaluated[Cylinder[{{y, 0, 0}, {y + 1, 0, 1}}, 0.1]]} ]; 0 test[x_, code_] := If[code == 1, Print[1], 2] test[anything,1] (*1*) test[anything,2] (*2*) 7 Does this do what you're after? ClearAll[test]; test[a_] /; Length[Stack[]] == 3 := 1 test[a_] := 2 test[134123] (* 1 *) Identity[test[134123]] (* 2 *) 1 + test[134123] (* 3 *) The value return by Stack[] in the condition is in the first example {test, Equal, Length} In the second, it is {Identity, test, Equal, Length} One can see that if test is ... 1 Like this? ClearAll[test] test[1] := Print[1] test[x_] := Return[2] test[1] 1 test[2] + 1 3 2 There is a mismatch in your definition, between x and x$ draw // Information Globaldraw draw[x\$_]:=Cylinder[{{x,0,0},{x,0,0}},0.1]... This is because With is a so called scoping construct. With tries to avoid conflicts between symbols. See this answer for a good explanation by Leonid. If you want to know all cases in which this happens, see this ...

5

When you do this, localization kicks in and renames one of the x to avoid conflict: This is in general beneficial, and designed to prevent trouble. In some situations, however, you don't want this localization-through-renaming to happen. In that case you can use Replace instead of With to replace y by x. I like to keep using the syntax of With ...

0

Nasser in his comment is referring (somewhat cryptically) to an undocumented second argument of Return. You would use it this way. test1[] := Module[{}, Do[Print[i]; If[i > 3, Print["i > 3"]; Return[100, Module]], {i, 1, 5}]; 200] However, I prefer to avoid undocumented stuff, so I recommend test2[] := Catch[ Do[Print[i]; If[i > ...

1

Like stated in the duplicate's link: functionexample[a_, b_, c_, x_] := Sin[a*x] + Cos[b*x] + Log[c*x] rootexample[a_, b_, c_, result_] := FindRoot[functionexample[a, b, c, x] == result, {x, 1, 12}, Method -> "Brent", PrecisionGoal -> 16][[1, 2]] a = 1; b = 1; c = 1; Quiet[Table[ Check[rootexample[a, b, c, i], "NaN", {FindRoot::bbrac}], {i, ...

3

I have used Sow/Reap in simple situations, but for what your proposing, I would usually follow your second idea. Something like this toy example: myfn[___]["Properties"] = {"a", "b", "function"}; myfn[x_, a_, b_, func_]["a"] := a; myfn[x_, a_, b_, func_]["b"] := b; myfn[x_, a_, b_, func_]["function"] := func; myfn[x_, a_, b_, func_][t_?NumericQ] := ...

4

I do not have a lot of first hand experience with this, as I've never took the time to implement a proper solution for this problem. Also, I don't know a lot about FEM methods. So what I am going to say is mainly based on observing how various Mathematica functions work. Don't use Sow/Reap for this I don't think Sow/Reap are designed with this ...

0

Since you are specifically interested in pattern matching rather than functional equivalents such as the one presented by eldo here is an additional answer. Szabolcs already gave my preferred solution, which is to use Alternatives, though he did not recommend it. Nevertheless I do. As complete code for reference: foo[rules : _Rule | {__Rule}] := ...

2

Edit This edited answer reflects comments made on my original version by the OP. Clear[rulelist, rule]; rulelist = {{tt -> 1, zz -> 1}, {tt -> 1, zz -> 2}, {tt -> 2, zz -> 1}, {tt -> 2, zz -> 2}}; Table[rule[i] = rulelist[[i]], {i, 4}]; To see where you went wrong let's look at what Derivative returns when it is given your version ...

4

I think your definition of d is not properly generalizable because the list dimensions don't match when doing higher derivatives. So I instead use a simpler definition of the Gateaux derivative from Wikipedia which does exactly the same thing as what you're trying to do. I call it gatD, and it takes the operator, the function u and a List of test functions. ...

4

Edit The definition of the Moyal product in the original question was missing a factor 1/n! under the sum. I used the textbook definition instead. A reference for this definition, without any unnecessary technicalities, is here: Quantum Mechanics in Phase Space (arXiv), see the appendix. Thanks to Rahul Narain for spotting the discrepancy between my ...

15

Ramblings Arguments of the left-hand-side head are evaluated in the course of function definition, therefore you can use a utility function that constructs the patterns that you want. For example: SetAttributes[nq, HoldFirst] Quiet[ nq[s_Symbol] := s_?NumericQ ] Now: ClearAll[f] f[nq @ a, nq @ b, nq @ c] := a + b + c Definition[f] f[a_?NumericQ, ...

Top 50 recent answers are included