Tag Info

Hot answers tagged

20

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.


16

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


13

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


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


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


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

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

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


5

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] :> System`Dump`AutoLoad[Hold[GeneratingFunction], Hold[GeneratingFunction, ...


4

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


3

This is how I'd do that: DynamicModule[{coords, edges, lines, centers, locators}, Dynamic[ Graphics[{ GraphicsComplex[coords, { {Line[edges]}, {Darker@Red, PointSize[0.02], Map[Point, Range[5]]}}] , MapIndexed[locators, lines] } , ImageSize -> 400, Frame -> True, PlotRange -> 2] , None] , Initialization :> ...


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

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

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


1

I don't think you need to define upvalues for f nor for your own myConjugate function. Using your definitions: ClearAll[F,f]; f[k_] := F[r] Exp[-I k r] all you need to do is tell Conjugate to distribute over addition and then Refine it by letting Mathematica know which variables are real. So, say you have an expression: Conjugate[f[k1] + f[k2] - ...


1

Here is a quick and dirty way to grab the data and tally it up: data = Gather[dateValuePairs, #1[[1]] == #2[[1]] &]; sumSet[list_] := {list[[1, 1]], Plus @@ (#[[2]] & /@ list)} sumSet /@ data (* {{3577478400, 55}, {3577564800, 55}, {3577651200, 55}, {3577737600, 55}, {3577824000, 55}, {3577910400, 55}, {3577996800, 55}, {3578083200, 55}, ...


1

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



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