Does anybody know if there is a built-in function in Mathematica for getting the lhs of downvalue rules (without any holding)? I know how to write the code to do it, but it seems basic enough for a built-in

For example:

```
a[1]=2;
a[2]=3;
```

`BuiltInIDoNotKnowOf[a]`

returns `{1,2}`

This seems to work; not sure how useful it is, though:

```
a[1] = 2
a[2] = 3
a[3] = 5
a[6] = 8
Part[DownValues[a], All, 1, 1, 1]
```

This is like `keys()`

in Perl and Python and other languages that have built in support for hashes (aka dictionaries). As your example illustrates, Mathematica supports hashes without any special syntax. Just say `a[1] = 2`

and you have a hash. [1] To get the keys of a hash, I recommend adding this to your init.m or your personal utilities library:

```
keys[f_] := DownValues[f][[All,1,1,1]] (* Keys of a hash/dictionary. *)
```

(Or the following pure function version is supposedly slightly faster:

```
keys = DownValues[#][[All,1,1,1]]&; (* Keys of a hash/dictionary. *)
```

)

Either way, `keys[a]`

now returns what you want. (You can get the values of the hash with `a /@ keys[a]`

.) If you want to allow for higher arity hashes, like `a[1,2]=5; a[3,4]=6`

then you can use this:

```
SetAttributes[removeHead, {HoldAll}];
removeHead[h_[args___]] := {args}
keys[f_] := removeHead @@@ DownValues[f][[All,1]]
```

Which returns `{{1,2}, {3,4}}`

. (In that case you can get the hash values with `a @@@ keys[a]`

.)

Note that `DownValues`

by default sorts the keys, which is probably not a good idea since at best it takes extra time. If you want the keys sorted you can just do `Sort@keys[f]`

. So I would actually recommend this version:

```
keys = DownValues[#,Sort->False][[All,1,1,1]]&;
```

Interestingly, there is no mention of the `Sort`

option in the `DownValues`

documention. I found out about it from an old post from Daniel Lichtblau of Wolfram Research. (I confirmed that it still works in the current version (7.0) of Mathematica.)

Footnotes:

[1] What's really handy is that you can mix and match that with function definitions. Like:

```
fib[0] = 1;
fib[1] = 1;
fib[n_] := fib[n-1] + fib[n-2]
```

You can then add memoization by changing that last line to

```
fib[n_] := fib[n] = fib[n-1] + fib[n-2]
```

which says to cache the answer for all subsequent calls.

Similar Questions

I want to implement this in Mathematica: I realize I can use ParametricPlot to get the lines, and then use the Mesh option to fill in the colors. Can anyone tell me what a general formula for the equa

I am writing my end term report on Mathematica. Can anyone explain or point me to a website/article on Related Work to Mathematica? I am asking in this forum because i can find that information nowh

I want to implement Newton-Raphson method in Mathematica. Here is my code: f[x] = x^3 - x^2 + 1 MetodaTangente[x0_, eps_] := Block[{p0, p1, dp, k}, p0 = N[x0]; p1 = p0; dp = 1; k = 0; While[dp > ep

Is there any way to profile the mathkernel memory usage (down to individual variables) other than paying $$$ for their Eclipse plugin (mathematica workbench, iirc)? Right now I finish execution of a p

How can I define a abstract odd function, say f[x]. Whenever f[x]+f[-x] appears, mathematica simplifies it to zero.

One may implement a limited form of Currying in Mathematica, using this construct: f[a_][b_][c_] := (a^2 + b^2)/c^2 Allowing one to do, for example: f[4][3] /@ Range@5 {25, 25/4, 25/9, 25/16, 1} Th

I am new to Mathematica and I am having trouble defining a certain composition of functions. Let m1 and m2 be maps such that m1: R^2 -> R^2 and m2:R^2->R^1 where R is the real line m1[eta_, zet

I'm a rookie of drools, learn about and work on drools for about 10 days. I was faced a problem that a Date() object is to be compared in a rule's LHS part. // in Java SimpleDateFormat format = new S

I'm reverse-engineering how Mathematica does list interpolation: (* Fortunately, Mathematica WILL interpolate an arbitrary list *) tab = Table[a[i], {i,1,100}] f = Interpolation[tab] (* get the coeff

I need to set an environment variable from a Mathematica notebook. Environment[VARIABLE] gives the value of the variable. But is it possible to set a variable, too?

I have a large file (written in Mathematica) that contains n records and each of these records is a list of fixed length m, where n > 10,000 and 500 < m < 600 (bytes). Note, my system does

I have this problem in Mathematica : L=16; f[x_]:=-x; mlat = Table[2 RandomInteger[] - 1, {L}, {L}]; ArrayPlot[mlat, ColorFunction -> (If[# == 1, White, Black] &), Mesh -> All] and I did t

Is there a way to define a function on a specific domain in mathematica? I need this because I want to invert a function which is only monotonic on a specific domain. For example: f[x_]:=x^2 g[x_]:=I

I don't know why Wikipedia lists Mathematica as a programming language with printf. I just couldn't find the equivalent in Mathematica. My specific task is to process a list of data files with padded

I randomly plotted a Sin[x] function in Mathematica 7 and this is what it shows: Note the visible defect at approximately x = -100. Here is a zoom of the defect part, clearly showing that Mathematica

I am trying to dynamically plot data contained in a matrix with Mathematica 7. The data is contained in it like this, obtained via a chemical model. [year H He Li C ... C8H14+,Grain- ] [0 0 0.03 0.009

I'm puzzled by this behavior of mathematica. The two following expressions should return the same result: Simplify[(1 - w)^2 Sum[w^(k+kp) Sum[If[l == lp, 1, 0], {l, 0, k}, {lp, 0, kp}], {k,0, \[Infini

I have exported matrix from Mathematica Export[all.txt, all] i.e. Matrix is s.t {{1,2,3,4},{1,2,3,4}} I tried to import it back, but it is not usable.. Import[text.txt] {1, 2, 3} {1, 2, 3} , Im

In mathematica (I am using mma 5.0 ( guess pretty old)), if I type the following as one line: Needs[Graphics`Master`]; Animate[Plot[Sin[n x], {x, 0, 2 Pi}, Axes -> False], {n, 1, 6, 1}] I then g

In Mathematica I have a list: x = {1,2,3,3,4,5,5,6} How will I make a list with the duplicates? Like: {3,5} I have been looking at Lists as Sets, if there is something like Except[] for lists, so I

I want to control the depth of the evaluations in Mathematica. For instance, consider the following simple code: y := 3.x z := 4.y v := 5.z Now, sometimes when I type v I would like Mathematica to re

Is it possible to create graphics of spherical co-ordinate system like this in mathematica or should I use photoshop? I'm asking because I want a high resolution graphic, but lot of the files on inter

Input: f[x_] := Sqrt[x^2 + y^2] f'[x] Output: x / Sqrt[x^2 + y^2] How do I get Mathematica to replace the denominator by f[x] itself? (Note: this is a simple example of a more complicated differenti

Since Mathematica 7.0, there is ParallelTry function [1] that evaluates multiple functions in parallel, returning the first result received. This is similar to parallelizing a search on a disjoint dat

This is part one of my attempt to find an answer to my question wireframes in Mathematica. Given a set of line segments how does one join two segments that are connected AND lie on the same line. For

I am doing a computing-intensive benchmark using Mathematica and its J/Link Java interface. The benchmark grinds to a halt if a memory footprint of about 320 MB is reached, since this seems to be the

Is there an existing function for the complex conjugate transpose in Mathematica? The equivalent in matlab is the to the apostrophe operator (').

The copy of Mathematica 8 I am using does not recognize the PlotLegend option for Plot and ListPlot. Specifically, it appears red when used. A quick online search turns out nothing buggy/unusual, so I

After playing with Mathematica's symbolic and numerical capabilities, I find it to be a decent programming language, too. However, something making it less appealing as a general-purpose language is t

Is there a way to select some text in a Mathematica notebook and then wrap the selection in brackets? For example, if I typed the following in a notebook: 1, 2, 3, 4 I want to be able to select all t

Is it possible to call functions from other notebooks in Mathematica, and if so how can I do it?

Absolute beginner question here. I have two lists in mathematica. The first one was generated by the Table command: Table[QP[[i]], {i, 10}] which generates the list: {52.5, 45., 37.5, 30., 22.5, 15.,

Has anyone come across a tutorial to create documentation for functions you create in Mathematica? I'm trying to organize some functions that I have written but so far I'm doing a terrible job. I woul

I have following code in Mathematica: rbar = 0.006236 rt = r_bar k = 0.95 sigmar = 0.002 betazr = -0.00014 sigmaz = 0.4 pi = 0.99 chi = 0.05 Cbar = -3.7 alpha1[n_] := alpha1[n] = alpha1[n - 1] + alpha

I posted the question yesterday and it seems obviously that some people are interested in given the count of upvotes. But it does not seem to be very useful, as some Mathematica guru puts it. However,

i try to get an Image out of Mathematica. I try to evaluate some Mathematica Code that uses Methods in Packages to generate Graphics. If i paste the Code to a Mathematica Notebook the Graphic is gener

So Mathematica is different from other dialects of lisp because it blurs the lines between functions and macros. In Mathematica if a user wanted to write a mathematical function they would likely use

I need to find the minimum of a function f(t) = int g(t,x) dx over [0,1]. What I did in mathematica is as follows: f[t_] = NIntegrate[g[t,x],{x,-1,1}] FindMinimum[f[t],{t,t0}] However mathematica hal

I'm trying to get a Mathematica example working. It's the one on Theo Gray's blog. I think that Mathematica must have changed since he wrote that code (May 2008), since I'm unable to get anything reas

Is there any way to get at the actual messages generated during the evaluation of an expression in Mathematica? Say I'm numerically solving an ODE and it blows up, like so In[1] := sol = NDSolve[{x'[t

I have a lot of files. Every of which contains data. I can happy import one file to Mathematica. But there are more than 500 hundreds of files. I do it so: Import[~/math/third_ks/mixed_matrices/1.da

I'd like to render Mathematica Graphics3D in three.js and avoid the frontend of Mathematica altogether.

I am trying to replicate Lagrange Interpolating Polynomials in Mathematica. The wiki for an explanattion of them can be seen here. This is my code (sorry I don't know how to place mathematica code in

How do you set up manipulate so that you can control a variable with a vertical slider instead of a horizontal slider in Mathematica?

I was taking a derivative in Mathematica and the result containted the term Sqrt', and I was wondering what the ' on the end ment? I believe in means 1/Sqrt from doing the derivative by hand but if so

I tried to follow the directions on autonumbering cells in a Mathematica-8 notebook, here http://reference.wolfram.com/mathematica/tutorial/AutomaticNumbering.html I created a tiny notebook with four

Mathematica 6 added TakeWhile, which has the syntax: TakeWhile[list, crit] gives elements ei from the beginning of list, continuing so long as crit[ei] is True. There is however no corresponding Dr

I'm very new to Mathematica. I want to use it as a data source for gnuplot (I know Mathematica can plot too), it uses a file format with data in columns and a space between each column on each row. Li

I want to add interactivity to Mathematica 3D graphics, other than with Manipulate which is cool but has its limitations. Think four example of a demo of the four cubes problem in Mathematica, a click

How can I use Java within Mathematica? I have two files,GRASP.nb and GRASP.java, in the same folder (BTW: GRASP=greedy randomized adaptive search procedure). The GRASP.java file contains a method pub