Tag Info

22

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.

18

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

14

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

12

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

11

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]

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

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

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

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

7

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

7

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

7

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

6

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

6

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

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

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

3

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

3

In contrast to symbols, part replacement with downvalues requires use of a temporary variable, (unless there is a better way). E.g.

3

I use this way of storing data extensively for what I do. I collected in my answer here http://mathematica.stackexchange.com/a/999/66 several ideas I developed over time around this type of structure. The Keys function based on what dreeves once submitted on StackOverflow ( http://stackoverflow.com/a/154704/884752 ) is what makes this structure practical as ...

2

Since no other answer has been posted yet I'll give my opinion. Using DownValues, which I believe is a hash table of sorts, is the normal and accepted way to store this kind of information in Mathematica, to the best of my knowledge. I cannot think of any real disadvantages compared to direct symbol assignment. Either form (direct assignment or ...

2

An alternative SetAttributes[getImmediateDownvalues, {HoldFirst, Listable}]; getImmediateDownvalues[sym_Symbol] := Internal`InheritedBlock[{sym}, Module[{tag}, PrependTo[DownValues[sym], HoldPattern[sym[] /; tag] :> Null]; TakeWhile[ DownValues[ sym], ! MatchQ[#, Verbatim@HoldPattern[sym[] /; tag] :> _] &]]] So ...

2

I do not know why Unprotectdoes not work (bug, feature?). The following does work though: ClearAttributes[GeneratingFunction, Protected] GeneratingFunction[2] = 3 GeneratingFunction[2] 3

2

One possibility would be to define a new Conjugate function, myConjugate, the behaves in the same way as Conjugate, except when it encounters a phase of the type Exp[+(-)I k r], it transforms it to Exp[-(+)I k r], leaving k and r as real variables. Another possibility (and the one I ended up using) is to go along the lines of this stack overflow answer and ...

2

This works: mp[2, 2] = 5 Block[{Plus = mp}, 2 + 2] (*5*) Now of course the interesting question is why Unprotect does not work as expected? I have not yet figured it out. Nothing special is seen in the Attributes. Actually doing Unprotect[Plus] Plus[2, 2] = 5 ??Plus shows the new rule.

2

I think the trick is to use a Function approach and memoize test itself, as follows. I'll use a slightly more complicated test function, and it will print EXPENSIVE! when the hard work is done: test[i_,j_] := test[i,j] = Function[x, Evaluate[Print["EXPENSIVE!"]; x^(i^j)]] f[i_,j_][x_]:= f[i,j][x] = test[i,j][x] f[1,2][x] (* EXPENSIVE! *) (* x *) ...

2

Will this approach work for you? If it will, then this is an answer :) test[x_] := Module[{a}, a = x; FullSimplify[Log[Exp[a^2]], Assumptions -> a > 0]] Clear @ "f*" (((ToExpression["f" <> ToString[#1] <> ToString[#2]][x_] := ToExpression["f" <> ToString[#1] <> ToString[#2]][x] = expr;) &) /. expr -> ...

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