Tag Info

New answers tagged

0

As Kuba suggested in his comment, With is helpful for accomplishing what you ask for. ClearAll[f]; SetAttributes[f, HoldFirst]; f[ip_] := Module[{b}, Column[{b = Total[ip], Row[{ Button["keep", AppendTo[op, {ToString[Unevaluated[ip]], b}]], Button["reject", AppendTo[op, {ToString[Unevaluated[ip]], Null}]]}]}]] SeedRandom[42]; ...


1

I'm not sure I understand the question, so will try this to see if it's in the right direction. Consider the following collection of definitions for a function f[ ] f[x_] := x^2; f[x_, y_] := f[x] + y^3; f[x_, y_, z_] := f[x, y] + f[x, z] + Sqrt[z]; f[x_, y_, z_, s_] := x y z s; Every time you add a new parameter, you need only define how you want it to ...


1

Description Today, I know this resourse http://library.wolfram.com/infocenter/MathSource/4557/ accidentally, in this notebook, I find the detailed useage of ListCorrelate,so I post here to share to more Mathematica user that need to improve their capability. Hope I hope sincerely others to edit and check it to make it complete and correct. Statement ...


4

diameter[x_List] := Max[EuclideanDistance @@@ Subsets[x, {2}]] diameter[pointset] (* 2 Sqrt[5] *) Now, let's improve the performance "a little bit". I believe the maximal distance will be realized at the points' convex hull (I'll not demonstrate it,but it's quite intuitive). Now, if you have a lot of points Mathematica provides a convenient and fast way ...


2

This will probably be closed as a duplicate of either: What are the use cases for different scoping constructs? Passing function as argument In the meantime the simple answer is that you need Module rather than Block, because the former creates a new Symbol whereas the latter merely temporarily changes the value of Symbol. myf = Module[{f}, f[3] = 33; ...


2

That integral can be done analytically: dist[mu_,sd_,kr_] = ProbabilityDistribution[ Assuming[ kr > 0 && sd > 0 && Element[{sd, x, mu, kr}, Reals], Integrate[(Sqrt[a]*sd)^(-1)*PDF[NormalDistribution[0, 1], (x - mu)/(Sqrt[a]*sd)]* PDF[GammaDistribution[kr, kr^(-1)], a], {a, 0, ...


5

[Not exactly an answer but possibly of interest and definitely too large for the margins..] Another way to do this is to reverse the order of summation. That is to say, there is an implicit sum over integer digits with an outer sum over integers. One could, in a sense, sum over the integers in the inner loop, and over digits in the outer. Well, sort of. ...


6

If you wish to compute the correct values using the method you have chosen you could specify Method -> "Procedural" for Sum: CumDigitSum[x_, b_] := Sum[DigitSum[n, b], {n, 1, x}, Method -> "Procedural"] CumDigitSum[1000001, 10] 27000003 However, the problem comes form the fact that Sum attempts to speed the calculation by finding a symbolic ...


8

The intuitive way to understand ListCorrelate is that the kernel is "moved" to every position in the array, and the sum of the products between the kernel values and the array values at that position is stored in the output array: (if the kernel is separable, i.e. if there are two 1d kernels so that Transpose[{k1}] . {k2} == k2d, then ListCorrelate can be ...


0

C, D, E, I, K, N, O are symbols already in use by Mathematica (ONCE KID) you do not follow the recommended convention that user variables are written lowercase. You can see that when typing the letter it is colored black, and not blue. most probably the Evaluate statement solves your other problem in your given metacode Manipulate[Plot[function[A, ...


1

Perhaps you can use DiscretePlot3D {alist, blist} = RandomReal[{-2 Pi, 2 Pi}, {2, 20}]; Manipulate[DiscretePlot3D[f[a + b], {a, alist}, {b, blist}, PlotStyle -> Hue[RandomReal[]], ExtentSize -> Scaled[.5]], {f, {Sin, Cos}}]


3

Why not define the expressions in such a way that their dependence on the independent variables is explicit: ClearAll[f, g, x, y] g[x_, y_] = x f[y] + y (* ==> y + x f[y] *) D[g[x, y], y, x] (* ==> Derivative[1][f][y] *) D[g[x, y], x, y] (* ==> Derivative[1][f][y] *) The mixed derivatives now agree.


6

I think you need to use a group-theoretical construction. In this way you will have full freedom in specifying any group of permutations you need. In your case the group is G = PermutationGroup[{Cycles[{{1, 2}, {4, 5}}], Cycles[{{1, 2, 3}, {4, 5, 6}}]}]; This generates a symmetric group on {1, 2, 3}, which also forces the same permutations on {4, 5, 6}. ...


0

I would do this by defining a preferred ordering to sort the first three arguments. In this example I will use canonical Mathematica Ordering but in principle you could use anything. I would define f[a_, b_, c_, A_, B_, C_] := With[ {order = Ordering[{a, b, c}]}, (* Get ordering of first three arguments *) f @@ { Sequence @@ ({a, b, ...


2

The method described in File-name completion for custom functions can be used to complete the "description" argument in your examples, but it won't work for the curried parameters and variables. Note that the method does not appear to support dynamic computation of completion choices, so it cannot be used to generate the list directly from a symbol's ...


2

You did not explain (at least to my satisfaction) what this code it supposed to do therefore is hard to give a rigorous answer, but I will attempt to cover multiple issues. First a note: Module[{}, . . . ] is arguably a purposeless construct, one I have no use for and which I remove with every opportunity I get. However at least one very experienced user ...


3

Why not just this? f[l_] := Block[{a,b,c}, a = Length[l]; b = First[l]; c = b/a; c*10] Then f[{2, 3, 4, 5}] 5


6

If you have Version 10 use the function Where in the GeneralUtilities package like this: Needs["GeneralUtilities`"] f[l_] := Where[a = Length[l], b = First[l], c = b/a, c*10] Then: f[{2, 3, 4, 5}] 5


2

For N[ _, 40] all inputs must have at least that precision. Your definition of Z[1], Z[2], and z[3] use weights with just machine precision. Rationalize the definitions. Z[1] = EmpiricalDistribution[ {0.5, 0.5} -> {0, 1}] // Rationalize; Z[2] = EmpiricalDistribution[ {0.6, 0.4} -> {0, 1}] // Rationalize; Z[3] = EmpiricalDistribution[ ...


0

Posting this as an answer because it's too long for a comment and it's a significant correction and amplification of @Hagus's answer. First, we need for options to be a BlankNullSequence to handle natural call forms where options need not be packaged in lists: ClearAll[constructorWithOptions]; constructorWithOptions[symbol_, options___Rule] := ...


5

I believe this results from Association being atomic (or "not NormalQ" as Taliesin puts it) without being fully overloaded to behave as a normal expression of equivalent structure. Observe: asc = <|"a" -> q, "b" -> r, "c" -> s|>; Block[{q = 1, r = 2, s = 3}, asc] <|"a" -> q, "b" -> r, "c" -> s|> Also: With[{q = 1, r = ...


6

I think your problem is probably related to the way OptionValue[name] is evaluated. When you use a regular List instead of an Association, you'll find the whole thing works. I don't have the mathematica-fu to understand why in detail, but here is an alternative solution that makes your function definitions pretty concise. Define a utility function that ...


2

ClearAll[base]; base = If[Head[#] === BaseForm, BaseForm[First @ #, #2], BaseForm[##]] &; FoldList[base, 63696, {16, 8, 2}]


2

You can define your own function that works with input the BaseForm of a number, sure: myBase[a_?NumericQ, base_Integer] := BaseForm[a, base]; myBase[a_BaseForm, base_Integer] := BaseForm[ FromDigits[ IntegerString @@ a, Last@a ], base]; so that 63969 // myBase[#, 16] & // myBase[#, 8] & gives you BaseForm[63969,8] but ...



Top 50 recent answers are included