Tag Info

Hot answers tagged

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


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


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


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


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


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


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


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


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


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


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$_] ...


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


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*)


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]


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


3

You can use SyntaxInformation. In this case, SyntaxInformation[Lim] = {"ArgumentsPattern" -> {_, _, OptionsPattern[]}, "LocalVariables" -> {"Limit", {2}}} does what you want.


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


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


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:


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


2

There is a mismatch in your definition, between x and x$ draw // Information Global`draw 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 ...


1

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


1

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


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


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


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 *)


1

You could also do this : With[{y = x}, SetDelayed @@ {Unevaluated[draw[x_]], Unevaluated[Cylinder[{{y, 0, 0}, {y + 1, 0, 1}}, 0.1]]} ];


1

Like this? ClearAll[test] test[1] := Print[1] test[x_] := Return[2] test[1] 1 test[2] + 1 3


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



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