# Tag Info

25

0. is an approximate real number that is very close to zero, while 0 is exactly zero. They aren't the same object, as 0 === 0. is False, and 0. has a head of Real, while 0 has a head of Integer. If you want to turn 0. into 0, you can use Chop, although that will replace all approximate numbers within some tolerance (by default 10^(-10)) of zero with 0.

21

General usage Here is what I think Using strings and subsequently ToString - ToExpression just to generate variable names is pretty much unacceptable, or at the very least should be the last thing you try. I don't know of a single case where this couldn't be replaced with a better solution Using subscripts is also pretty bad and should be avoided, except ...

19

To match all sorts of zero, you can use F[A___, zero_ /; zero==0, B___] := 0 Another possibility which catches more cases, but also matches some non-zero expressions is to use PossibleZeroQ: F[A___, _?PossibleZeroQ, B___] := 0

15

How about this pattern instead: F[A___, 0 | 0., B___] := 0 Now you get zero in both cases. Regarding the explanation: You obviously know that the 0. comes about when doing numerics, and the reason is that in numerics we're working with approximate real or complex numbers. These are to be distinguished from exact numbers of which 0 is an example. Both ...

13

DownValues[f] = DeleteCases[DownValues[f], _@_[_?NumericQ, _?NumericQ] :> _] {HoldPattern[f[]] :> 0, HoldPattern[f[1]] :> 1, HoldPattern[f[2]] :> 2, HoldPattern[f[3]] :> 3, HoldPattern[f[1, 2, 3]] :> 6, HoldPattern[f[x_, y_]] :> x y} It is possible to make this pattern fail if you want also to clear something like: f[1, 2] /;...

13

I think it is worthwhile to include a table of how different methods compare when deciding about possible zero value. My advice is to use PossibleZeroQ, but always make sure to handle/be prepared to all extrema. Let me quote the documentation: The general problem of determining whether an expression has value zero is undecidable; PossibleZeroQ provides ...

11

You can use the third argument of ToExpression to do this in a structured way: ToExpression["x", InputForm, Unset]

11

It seems that f[Except[0, a_], Except[0, a_]] isn't considered doubled argument while this f[a : Except[0], a : Except[0]] is. So the former isn't considered special case and is overwritten by your next definition. I can't explain properly but seems to be expected. ClearAll[f]; f[a_, a_] := 0 (*or f[a : Except[0], a : Except[0]] if f[0,0] should give f[...

9

This might be the general sort of operation you seek. keyvalpairs = DownValues[arr] /. Verbatim[HoldPattern][arr[k_]] :> k (* Out[121]= {1.5 :> 0.4, 3.5 :> 0.7, 7 :> 0.3} *)

9

The two key functions you need to know to do what you want are: ValueQ to test if your symbol has a value or not and DownValues to get a list of rules for the definitions (some kinds, anyway) With these, here's a barebones implementation that should give you the idea. Here a is the database, f is the function to query values from a (and set it if not ...

8

The form f[a_, b_][t_] allows you to conveniently use f[a, b] as if it were a function. For example, you can Map it over a list: f[a_, b_][t_] := a^t + b f[2, 3] /@ {4, 6, 8} {19, 67, 259} Please see Define parameterized function for details about this format and alternatives. See also What is the distinction between DownValues, UpValues, SubValues,...

8

CirclePlus is a built-in symbol already with no meaning for the kernel, but meaning in the front-end. The second definition tried to use the first definition (with head 'Function'), which is protected. (Note the pattern [a_,b_] appearing in the error message, which tells you the left-hand-side is the issue.) Just one line is enough as @rm-rf said, like ...

8

Define a default or background for the hash I would like to propose a different approach, which is to define a default or generic value for a. If there is not a consistent background value you may use: a[_] = "Empty"; If[a[1/2] =!= "Empty", a[1/2] += 1, a[1/2] = 1] If you can define a constant (or structured) background for your table such as 1, you ...

8

Here is one way to do it: {arr[1.5] = 0.4, arr[3.5] = 0.7, arr[7] = 0.3} Total[DownValues[arr][[All, 2]]]

8

Another solution for this weird exercise is to make a combination from using $Pre and defining a new plus function. You use$Pre to replace every occurrence of Plus by your own definition which only act special at the input plus[2,2] and calls the normal Plus otherwise: SetAttributes[plus, Attributes[Plus]]; Unprotect[plus]; plus[2, 2] = 5; plus[args___] := ...

8

Another way to get two definitions is to only use Except on the first argument for the case where the arguments are identical. Clear[f] f[Except[0, a_], a_] := 0 f[Except[0, a_], Except[0, b_]] := (a - b) Log[a - b] Annoyingly it doesn't matter the order for the definitions it comes out like this: DownValues@f (* {HoldPattern[f[Except[0, a_], Except[0, ...

8

In keeping with the multiple DownValues part of the question, this is an option: ClearAll@f conditions[x_] := {Mod[x, 2] == 0, Mod[x, 3] == 0, x < 10} f[x_ /; Or @@ conditions[x]] := StringJoin@Pick[{"Fizz", "Buzz", "Zapp"}, conditions[x]] f[x_] := x It tests for any of the conditions first, and then within the body of the function uses the individual ...

7

Sometimes I define my pattern using a tolerance value to consider zero: tolerance = 10.^-15; F[A___, zero_ /; Abs[zero] <= tolerance, B___] := 0; As said before, using Chop is possible too (and you can use the second argument to set the tolerance): F[A___, zero_ /; Chop[zero, tolerance]==0, B___] := 0; And if you want to take more cases (like the ...

7

GeneratingFunction by default is not a function: it is a stub which loads corresponding .mx package. You can see this with the following: ClearAttributes[GeneratingFunction,{Protected,ReadProtected}] OwnValues@GeneratingFunction {HoldPattern[GeneratingFunction] :> SystemDumpAutoLoad[Hold[GeneratingFunction], Hold[GeneratingFunction, ...

7

How about this ;-) \!$$\*InterpretationBox[2,3]$$+2

7

Using DownValues enables you to format the display in the subscripted form without using Notation and Symbolize (Format[#[n_]] := Subscript[#, n]) & /@ {x, \[Sigma], a}; kvar[k_] := Through[{x, \[Sigma], a}[k]] kvar[3] kvar[n] If you will never use a symbolic index then you can restrict the argument of kvar to Integer as you did originally.

6

You can use FreeQ and select only those down-values that are free of Pattern: Select[DownValues@f, FreeQ[#, Pattern] &] (* {HoldPattern[f[1]] :> 1, HoldPattern[f[2]] :> 4, HoldPattern[f[3]] :> 9, HoldPattern[f[1, 2]] :> 5, HoldPattern[f[3, 4]] :> 25} *)

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

It doesn't work because it's simply not correct syntax. String patterns and expression patterns are not interchangeable. Each works only with its own set of functions: string patterns work only in StringMatchQ and expression patterns only work in MatchQ. In function definitions you can only use expression patterns. You can use something like this instead:...

5

It's bad practice to have your functions depend on global symbols like x buried in their definitions, for Mathematica under certain conditions rewrites function parameters as x$_ to avoid clashing with the global symbol. Use Trace on the OP's examples to see that in the second one, x_ is rewritten as `x$_. See the tutorial Variables in Pure Functions and ...

5

If I understand your question correctly you want to transform a list e.g. {a,b,c,d} into DownValues of a Symbol with some name e.g. array1. Sometimes this is called an indexed variable (although I think this term is misleading). The following function does just that: toDownvalues[name_, list_] := MapIndexed[(name[First@#2] = #1) &, list] ClearAll@...

4

To start the discussion I'll look at timings. A simplistic test indicates that the DownValues method is somewhat faster, and this agrees with my past experience as well. f1[a_, b_, c_, d_] := f1[{a, b, Max[c, 0], Max[d, 0]}] f1[{a_, b_, c_, d_}] := a + b + c + d f2[a_, b_, c0_, d0_] := With[{c = Max[c0, 0], d = Max[d0, 0]}, a + b + c + d] a = ...

4

This does not answers my own question fully (I am still interested to see if someone might come up with an truly elegant solution based on DownValues) but I found a rule-based solution that is imho. elegant non the less. fizzbuzz[rls_]:= With[{res=ReplaceList[#, rls]}, If[res=={}, #, StringJoin@res]]& fizzbuzz[{_?(Mod[#,2]==0&) -> "Fizz", ...

4

I think Save is the built-in function that come closest to what you are looking for. a = 5; n = 1/2; f[x_] := a + x^2 Save["stdout", f]

3

This is how I'd do that: DynamicModule[{coords, edges, lines, centers, locators}, coords = {{1.08, 0.94}, {1.08, 0.036}, {0., 0.97}, {0., 0.}, {1.94, 0.49}}; edges = {{1, 2}, {1, 3}, {2, 5}, {3, 4}, {4, 2}, {5, 1}}; lines = (coords[[#]] & /@ edges); centers = .5 (# + #2) & @@@ lines; locators = With[{i = #2[[1]], p1 = #[[1]], p2 = #[[2]]...

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