Questions on correct (or improved) formulation of Mathematica code to achieve particular results. Use this tag (not "symbol") for questions about all those odd @@ /@ # & and ~ characters.
41
votes
2answers
932 views
Flatten command: matrix as second argument
One thing I could never wrap my head around is how Flatten works when provided with a matrix as the second argument, and the Mathematica help isn't particularly ...
35
votes
2answers
809 views
Head and everything except Head?
I have been working on picking expressions apart using Head and Part and encountered a little mystery. Consider the canonical ...
31
votes
6answers
1k views
Functions vs. patterns
Every time I define a new function, I wonder which construct I should use: true functions obtained by using Function, or rule-based syntax. For example, these are ...
29
votes
3answers
513 views
Usage of \[InvisibleApplication] and other related invisible characters
From the front end, \[InvisibleApplication] can be entered as Esc @ Esc, and is an invisible operator for @!. By an unfortunate ...
25
votes
3answers
495 views
Parentheses in pure functions: # & vs. ( # &)
I've been using Mathematica for years, and over time I have developed the habit of using:
Select[data, (# == 1 &)]
instead of
...
23
votes
1answer
468 views
Convenient string manipulation
With Mathematica I always feel that strings are "second class citizens." Compared to a language such as PERL one must juggle a lot of code to accomplish the same task.
The available functionality is ...
21
votes
5answers
334 views
Constructing symbol definitions for With
I would like to be able to define two arrays, one containing symbol names and one containing the values of those symbols, for use in constructs such as With. For ...
21
votes
2answers
553 views
Meaning of backtick in floating-point literal
If I compute, say, 1/3//N, Mathematica displays
0.333333
as the result.
When I copy that output to use elsewhere,
the paste ...
21
votes
4answers
531 views
Why is there no PositionFunction in Mathematica?
Too often I have seen the programs of inexperienced users greatly slowed by using Position in an iterative fashion, when far faster would have been to compute a ...
18
votes
1answer
344 views
Why doesn't PatternTest work with Composition?
While playing around with the solutions to this question, I've found some very strange behaviour:
...
17
votes
4answers
1k views
Is there a syntax for single-line comments for notebooks?
I'm aware that I can use (* ... *) to comment out stuff in a notebook. Many languages have a syntax for single-line comments, such as
...
17
votes
5answers
344 views
Change syntax from other programs to mathematica syntax
Is there a good way to transform syntax from other computer algebra systems or from latex to mathematica syntax?
17
votes
2answers
726 views
Code Readability and Object-Oriented Code
The basic problem I have been running into is making readable code, where in other languages I am more familiar with I might have been using class or struct like objects. For example, you can imagine ...
17
votes
2answers
523 views
Why there is no name for #1@#2&?
Why does the pure function #1@#2& have no name? Is it due to the way Mathematica works, or is it "just because"?
I'm asking because I find inelegant to write ...
16
votes
1answer
309 views
Prefix operator with low precedence
The question is simple, but I will elaborate on the background as well for those interested in the idea:
How to define a new operator with specified precedence value?
Background
Mathematica was ...
15
votes
4answers
1k views
Clear complains that a variable is not a symbol or a string?
Here's a small listing where I've used EscqEsc to typeset θ in the notebook:
...
15
votes
3answers
695 views
Why does Mathematica use [[ ]] notation for array indexing?
I am confused by why Mathematica uses [[3]] to get the 3rd element, or [[i,j] to get the i,j-th element of a 2D array.
This ...
14
votes
2answers
286 views
Distributing PlusMinus consistently
I would like to give the built-in function PlusMinus, which is undefined by default, the intuitive meaning: considering all possible combinations of $+$ and $-$.
...
12
votes
7answers
426 views
Equating matrices (or higher order tensors) element-wise
Say I have two matrices (or, as in my case, higher order tensors) $A,B$, and I want to solve the equation $A=B$. To do so , I need a list of equations that equate entry-wise the elements of $A$ and ...
12
votes
2answers
248 views
What kind of hypergeometric function is it?
I found a formula for an integral of a product of three Bessel functions at The Wolfram Functions Site:
I cannot understand what kind of hypergeometric function it is.
The Mathematica code given ...
12
votes
2answers
280 views
How to embed an image into a string?
The documentation for String contains the following statements:
Strings can contain any sequence of ordinary and special characters:
…
Strings preserve ...
11
votes
3answers
214 views
Numbers in alternate bases transcend the evaluator?
It looks to me like a number in a base other than base 10 gets evaluated before the evaluator ever gets a chance to be tweaked.
For example, FullForm[16^^abcdef] ...
11
votes
4answers
159 views
Constraint syntax compaction
Is there a more compact way to represent these constraints:
NMaximize[{a+b+c,...
11
votes
2answers
233 views
Convert operator to string
How to programmatically convert an operator such as Equilibrium into its displayed form as a string? The conversion should work like this:
...
11
votes
3answers
155 views
Why can't I use Sequence to perform a Select like task?
Suppose I wanted to write down a list of $p^2$, for $p$ a prime between $1$ and $20$. I would expect
Table[If[PrimeQ[k], k^2, Sequence[]], {k, 1, 20}]
to work. ...
10
votes
3answers
342 views
With versus Function
I've seen people compare Block with Module and With as scoping constructs, and try to figure ...
10
votes
4answers
312 views
Extracting equations from Piecewise expressions
Say I have a PDF:
PDF[LogNormalDistribution[1.75, 0.65], x]
Calculating it, Mathematica gives me an expression that looks like this:
I want to extract the ...
10
votes
2answers
213 views
Functions that remember their values
Can someone explain what is going on with the following ...
func[y_] := func[y] = (Print["Hello world !!!"];)
func[1]
Hello world !!!
...
10
votes
3answers
462 views
Working with PhysicalConstants
I recently found the PhysicalConstants` package. First I thought this was nice, since I didn't have to look up some quantities all the time and copy them into the ...
10
votes
3answers
273 views
Why does ++++x return an increment of 2 when the value of x is only incremented by 1?
This line returns 3:
x = 1; ++++x
However, the value of x after the increment is only ...
10
votes
2answers
355 views
How do you programatically load data into symbols?
I am trying to convert a list of string names into symbols, which will then be used to store data. I have 24 files (where the name of each file is a member of the list mentioned above) that I need to ...
10
votes
2answers
378 views
Representing second derivatives with a double overdot
I would like to express a second derivative with two dots above the variable. You can do this with a first derivative by saying:
...
10
votes
1answer
109 views
Infix form of PutAppend ( >>> ) does not work with variable
I'm new to Mathematica, so I suspect this question involves either a misunderstanding involving variables or the usage of >>>.
On a webMathematica page ...
10
votes
2answers
101 views
Functions in a different context are replaced with infix forms of their namesake in System`
So, in an attempt to help out Fabian with his question, I did what I often do, and created a new symbol in the Pillsy` package that I could play with. Since it was ...
10
votes
2answers
354 views
Flatten at a certain level
In an earlier question the brainteasing fourth syntax variant of Flatten was discussed. I see that with
...
9
votes
4answers
265 views
Calling Table with custom iterator
I often find myself in situations where I, for example, need to build a table for some expression, but want to set the number of points rather then the step size, so the code ends up looking like this
...
9
votes
2answers
302 views
How can one define an infix operator with an arbitrary unicode character?
You can setup "UsefulFunction[a, b]" to use custom infix notation "a [LeftRightArrow] b" as follows:
...
9
votes
1answer
361 views
8
votes
4answers
337 views
Error when using rule as a list index - { i, x[[i]] } /. i -> 5
I can't seem to use a rule to index a vector.
x = Range[10];
{ i, x[[i]] } /. i -> 5
I get the following error when evaluating the above code, even if the ...
8
votes
5answers
352 views
can the color in MeshStyle be specified by a ColorFunction, such as “SunsetColors” for example. If so, what is the correct syntax?
This is an example given in Help:
Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, MeshStyle -> Gray]
Could this be changed to something like
...
8
votes
1answer
141 views
Accept Infinity as an Integer argument
I have the following function, albeit contrived:
Foo[x_Integer] :=
Module[{i},
i = 1;
While[i <= x && i <= 10, i++];
i - 1
];
I am ...
8
votes
2answers
192 views
Cases[data,Colon[key,_]] vs. Cases[data,key:_] toward XPath, XQuery
Exploring methods to index and search tree-structured key:value pairs via named-entity index (key paths) as opposed to Position-based indexing.
Using Rule as ...
8
votes
2answers
545 views
Collapse a section of a huge function
(I have recently taken to Mathematica... still a noobie)
Is there an elegant way of hiding/collapsing only a portion of a function (for example, in Matlab, within a function, one can collapse the for ...
8
votes
1answer
127 views
Different behaviours of Default Argument
I don't really understand the behaviour of Default Argument. If I execute this command in Mathematica:
In: {f[a], f[a + b]} /. f[x_ + y_.] -> p[x, y]
...
7
votes
5answers
1k views
Notation of partial derivative
I want to write partial derivatives of functions with many arguments. Why is it that when I type
f[x,y] ctrl+6 (0,1)
it turns out to be bad syntax? The output of
...
7
votes
2answers
172 views
Internal Representation of ?name and ??name
Code
FullForm[Hold[?name]]
Syntax::bktmcp: Expression "Hold[?name]]" has no closing "]".
Syntax::sntxi: Incomplete expression; more input is needed .
Question
...
7
votes
3answers
280 views
Why can't D[] be used in place here? [duplicate]
Possible Duplicate:
General::ivar is not a valid variable when plotting - what actually causes this and how to avoid it?
Beginner question:
Why can't I use ...
7
votes
3answers
212 views
how to read in a file in the same directory?
Now I am writing a small package. I have written several files and put them in the same directory. And some files need to read in or include another file in the same directory. But when I simply write ...
7
votes
2answers
158 views
“A number in base b” and pure functions: why doesn't b^^# & work?
Why do the following not work (in Mathematica 7)?
2^^ # & /@ {1000, 1101}
and
2^^ # & @ 1101
This does work:
...
7
votes
3answers
181 views
Is it possible to define custom compound assignment operators like ⊕= similar to built-ins +=, *= etc?
I would like to do something like this:
...