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.

learn more… | top users | synonyms

24
votes
5answers
2k views

Clear complains that a subscripted variable is not a symbol or a string?

Here's a small listing where I've used EscqEsc to typeset θ in the notebook: ...
23
votes
1answer
442 views

Why doesn't PatternTest work with Composition?

While playing around with the solutions to this question, I've found some very strange behaviour: ...
59
votes
2answers
2k 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 ...
46
votes
6answers
2k 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 ...
42
votes
2answers
1k 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 ...
10
votes
1answer
1k views

What is the difference between Set and SetDelayed?

I have a problem with defining a function and most of time I get confused by Set or = and ...
7
votes
3answers
1k views

Using D to find a symbolic derivative

I need to do the following: Define a function Take the derivative of this function and have a look at the symoblic representation Substitute in some values With the bonus that I want to use the ...
26
votes
2answers
1k 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 ...
6
votes
2answers
564 views

Evaluating an If condition to yield True/False

I would like to decide whether an option passed to my custom function has the value Automatic or something else. This is my attempt: ...
9
votes
2answers
3k views

What does # mean in Mathematica?

I asked Mathematica to compute the following Solve[c (1-x)^2-x^(1/4) == 0, x] and it returned this: x = Root[#1^8 c^4 - 8 #1^7 c^4 + 28 #1^6 c^4 - 56 #1^5 ...
23
votes
1answer
548 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 ...
29
votes
5answers
938 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 ...
9
votes
2answers
599 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: ...
6
votes
3answers
759 views

Keep function range as a variable

Plot[2*x^2 - x + 2, {x, -1, 1}] plots a function of x from -1 to 1. As far as I can see, I cannot "save" this range in a variable: ...
25
votes
6answers
494 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 ...
17
votes
3answers
581 views

How is + as an infix operator associated with Plus?

How is + associated with Plus? Suppose I want to define a head FooBar and have it ...
30
votes
4answers
685 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 ...
13
votes
3answers
678 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
1answer
207 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 ...
6
votes
1answer
697 views

Coding mistake? [closed]

I have just started using Mathematica with v9.0. I am trying to follow a computation from a book on Fourier series with the function $f(x)=x$ on the interval $-\pi < x < \pi$. Here is the code ...
29
votes
3answers
655 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 ...
6
votes
5answers
303 views

Is it possible to insert arguments into functions when they're used like Function@ or //Function?

Is it possible to insert arguments into functions when they're used like Function@ or //Function? Sometimes I need to do this, but then I have to return to the standard function usage: Function[] ...
5
votes
2answers
250 views

Splicing a list of arguments into a function with Sequence

Not sure if this has been asked, but I have a fairly simple operation that I don't know the syntax for. Say I have an array with some values, and a function f that accepts an arbitrary number of ...
2
votes
4answers
414 views

Using the result of Solve in subsequent calculations

I'd like to know how to use the results of Solve in later calculations. Here's what I'm doing now: This is my expression ...
25
votes
1answer
617 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 ...
15
votes
1answer
311 views

Shorter syntax for Fold and FoldList?

I find it tedious to write out the following: a = {1, 2, 3, 4}; Fold[f, First @ a, Rest @ a] f[f[f[1, 2], 3], 4] It ...
12
votes
3answers
430 views

With versus Function

I've seen people compare Block with Module and With as scoping constructs, and try to figure ...
10
votes
2answers
453 views

How to create custom Graphics primitive?

How to create custom Graphics primitive? It should have the following properties, resembling properties of built-in geometric figures, like ...
5
votes
2answers
153 views

Syntax highlighting conflict

I'm trying to syntax highlight this expression: x=$\sqrt{\pi }$//N and from various sources I've learned that x (the assigned symbol) belongs to context Global while the Pi and N belongs to System. So ...
5
votes
3answers
313 views

Syntax for prefix mode with multiple arguments using @ shorthand

Is it possible to use the @ symbol with multiple arguments? The Prefix command suggested not. If so, why?
33
votes
1answer
657 views

Incompatible Changes since Mathematica Version 7?

There is documentation of the incompatible changes made to Mathematica with each major release: Incompatible Changes since Mathematica Version 1 Unfortunately that documentation simply stopped ...
10
votes
1answer
547 views

Explain a Mathematica winning one-liner

...
8
votes
5answers
649 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 ...
22
votes
2answers
699 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 ...
5
votes
4answers
4k views

Mapping multiple parameters of a function to specific values

I am a little bit confused by the documentation for Apply (@@). I want to define a list of parameters, for instance: ...
11
votes
4answers
500 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 ...
8
votes
2answers
243 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 ...
6
votes
2answers
1k views

How to reverse the direction of the y axes

I have a similar question to this one. Unfortunately, the answers there didn't help me. I would like to plot a simple function with the normal Plot function. ...
5
votes
2answers
890 views

Constructing a Summation with a Variable Number of Inner Sums

I have a question regarding multiple sums. And my second index depends on the first index. Here it is: $$ \sum_{d=1}^{P}e^{-d}\sum_{\substack{1\leq k_{1}\leq Q \\ 1\leq k_{2}\leq Q \\... ...
0
votes
0answers
60 views

My “N” turned red randomly [duplicate]

For some reason my N function showed up in red even though I haven't noticed it in that color before. The color actually makes the code much more readable but I just don't know if I should be alarmed ...
35
votes
5answers
1k views

V10's Operator Forms - what are they good for?

V10 introduces an operator form for several functions perhaps primarily due to their role in queries as part of introducing data science functionality. At first pass it seems a lot of effort to add ...
20
votes
2answers
1k 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 ...
8
votes
1answer
493 views

What is the difference between prefix/postfix notations and map?

I am new to Mathematica and just experimenting with the different programming constructs. I have been looking at Map and how to evaluate a function for a list of ...
12
votes
7answers
739 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 ...
9
votes
5answers
3k 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 ...
8
votes
3answers
549 views

Difference between @ and @@

I am confused by the commands @ and @@. From the documentation, I learnt that @@ is the ...
8
votes
1answer
152 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] ...
5
votes
3answers
1k views

Matrix Multiplication in context of row and column vectors

I've been looking at some matrices in Mathematica and I've noticed something very weird: They're extremely temperamental when it comes to dot products! For example, if I have the following, ...
5
votes
1answer
313 views

What does ` symbol in ClearAll[“Global`*”] mean?

I have found an example for clearing all symbol definition by: ClearAll["Global`*"] But what does the symbol ` mean? It isn't the apostrophe ', yet it is not ...