For questions on writing functions (pure or using Set/SetDelayed) for any purpose, including the features that may be incorporated in those functions, such as options, patterns and conditions.

learn more… | top users | synonyms

3
votes
0answers
65 views

How to match the arguments of the function in any order

The issue is the following, with a dumb function with (4) arguments of (4) different heads (for each head is always no grater than one arguments): ...
1
vote
1answer
68 views

Constructing an I-graph

I am attempting to build I-graphs in MMA, i.e. the non-Generalized Petersen graphs, and I found some code from Eric Weisstein, but this is now outdated and doesn't run. I have attempted to switch the ...
6
votes
1answer
134 views

Graphics Function Definition Style (Using SubValues vs. DownValues)

I am going through some of the items in "Modern Differential Geometry of Curves and Surfaces with Mathematica, Third Edition". An example of the style used by these authors is: ...
1
vote
1answer
71 views

Retrieve XML Tree with only specific XMLElements

I would like to retrieve the XML Tree of a XMLObject with only specific XMLElements. In the help of Mathematica ...
51
votes
7answers
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 ...
13
votes
2answers
231 views

Clean and handy Options management/filtering

the case I want to be able to create a function with some default options but also without need to add full explicit list of options available for it. And then inside I want to be able to filter ...
3
votes
2answers
75 views

General function for the expansion of a polynomial of operators

This question is motivated by a Quantum mechanical problem - but in explaining the problem - I assume no knowledge of quantum mechanics. I want to define a function that can expand and simplify the ...
6
votes
2answers
100 views

Optional arguments with lists as function parameter

I'm developing a function f which should support optional arguments {x_, y_} in list form. A simple example looks like this: ...
2
votes
2answers
77 views

Creating a bump function on a spline region [closed]

For a presentation, I'd like to include a 3D plot of a function of two variables x,y with the properties that (a) it looks "smooth" i.e. without jagged edges and (b) has support equal to a given ...
1
vote
2answers
55 views

Subsituting expressions for expressions

I'm trying to define functions using other functions, like so: f[a_, b_] := a/b; a[c_] := c; b[d_] := d; Now what I want to be able to do is two things: ...
3
votes
2answers
91 views

How to use an expression for the argument to a function [duplicate]

I would like to define a function over a list of variables like f[a_,b_,c_]:=a+b+c However, when this list is big it makes uncomfortable to read. Can I somehow ...
0
votes
2answers
72 views

Plotting vectors using derivatives

This question stems from a previous question I asked here: Multiple Integral of equation including dx prime? An equation describing a gravitational field of a cuboid is given as: $V(X,Y,Z) = -G\rho ...
0
votes
0answers
46 views

Multiple Integral of equation including dx prime?

I'm hoping someone can verify I've written this function properly since I'm a little new to Mathematica. What I'm attempting to do is code a function that describes a gravitational field of an ...
2
votes
0answers
54 views
4
votes
6answers
408 views
5
votes
1answer
90 views

Is Sow/Reap compatible with Composition?

I'm trying to figure out why these two pieces of code return different results. As expected, f[Reap[Sow[x]]] gives ...
0
votes
1answer
42 views

How to have default case in Which? [closed]

Saying we have a function: a[x_] := Which[x === 1, "AAA", x === 2, "BBB", _, $Failed] How can I make this function to be equal to ...
8
votes
4answers
1k views

Turn expression into function

I'm new to Mathematica and I spent the last hour trying to make the expression ...
8
votes
1answer
116 views

Maximum number of literal values for SetDelayed?

Under what circumstances should I expect Mathematica to turn a delayed assignment into an immediate assignment? I have put together a minimal example (available here) which demonstrates this behavior. ...
0
votes
1answer
91 views

How can I generalize this interpolation function? [closed]

I have a data set where I want to evaluate a manual interpolation function. The part shown below expands up to the 7th power currently. I was wondering if it was possible to generalize this part of ...
2
votes
3answers
89 views

How do I map a function onto the result of an expression that already has a pure function as an argument

I admit that I have only a basic understanding of the way Mathematica handles pure functions - enough to be dangerous, unfortunately. I have some data that is in a list of lists of lists. I would ...
2
votes
1answer
57 views

Is there a shorthand for Optional with Condition?

I have a recursive function that does some work to an object for a repeatedamount of times. ...
1
vote
2answers
72 views

How to make function accept list of rules as an argument?

Considering the list of rules: r= {"a" -> "1", "b" -> "2", "c" -> 3, d -> 231, "e" -> 1.25}; I want to have a function ...
0
votes
0answers
39 views

Using list of values as argument in a function [duplicate]

could you please help me with a task. I would like to define a function with a list of values as an argument. There is a working code with a values {1,1,1}: ...
1
vote
2answers
206 views

Minimizing the largest difference between two functions

I have two functions defined on the same domain, $f(x)$ and $g(x)$ where $x$ is in $(0,1)$: f[x_] := (-1 + x) Log[1 - x] - x Log[x] g[x_] := Log[1 + 2 (-1 + x) x] ...
2
votes
1answer
130 views

What is the meaning of two square brackets in function definition? [duplicate]

I came along the following function definition f[a_, b_, c_][r_] = ... with some function on the right hand side. Is this equivalent to ...
0
votes
0answers
36 views

How to create a big jacobian matrix function over some variables of a vector function?

Suppose I have a vector function of $n$ components and it has $n+m$ arguments, $$\vec{f}(x_1,x_2,\dots,x_n, y_1, y_2, \dots, y_{m})$$ where $n, m $ are around 10. I want to create a Jacobian matrix ...
0
votes
0answers
25 views

Function with variable arguments [duplicate]

I wrote a function with three arguments like this f[a_,b_,c_]:=... When I call this function MMA requires the input of all three arguments (e.g. f[1,2,3]). Is ...
1
vote
2answers
46 views

Retrieve Options from function other than graphics objects

We can retrieve information about used Options for Graphics via: Options[Plot[Sin[x], {x, 0, 1}], "AspectRatio"] (* {AspectRatio -> 1/GoldenRatio} *) even if ...
7
votes
2answers
151 views

How to track Part of Symbol or How to symbolize Parts of Symbol without copying data

Let's create simple function: SetAttributes[h, HoldFirst]; h[data_] := Dynamic[Grid[data], TrackedSymbols :> {data}] test = RandomReal[1, {2, 2}]; h[test] ...
-1
votes
1answer
52 views

Create a list of all functions given a set of arguments

I'm trying to examine some patterns on the permutation of elements in finite element functions. I'd like to create a list of all functions (256 of them) on ...
2
votes
2answers
84 views

Dynamic updating of Slot number [duplicate]

I want to change the slot number inside Manipulate[]. Here is a minimal example which does not work: ...
9
votes
1answer
155 views

Map at a subsequence of a list

I'm trying to construct a function somewhat similar to MapAt. A basic usage example should look like this: ...
-1
votes
1answer
75 views

I have several functions and how I know which one is the maximum in a certain range?

What I did are the followings These lines will generate a piecewise function pw which tells me the corresponding variable given an interval as follows: Now, I want to get: names of variable ...
0
votes
1answer
87 views

Simplifying code

I have a program where I solve the Lorentz-63 system (system of ODEs). I wish to generalize my program to make my code cleaner. Is there any way to create a function that works in a fashion such as ...
0
votes
0answers
56 views

Piecewise function output response interpretation

Piecewise[{{t^2, t <= 0}, {t, t >= 0}}] Outputs: t^2 t <= 0 t t >= 0 0 True What does the last line of output 0 True mean? Sometimes it ...
1
vote
1answer
47 views

Defining functions that depend upon other functions which depend upon other functions and so on

The problem is that I have to evaluate functions, that take output from previous functions, and combine them to form new functions, and do some operations and evaluate the value at new function. But, ...
0
votes
1answer
59 views

How to define the following bracket?

My quetion is about the definition of the so-called Courant Bracket. Given two generalised vector field, defined as a formal sum of a vector and a one-form, i.e. $X = x + \lambda,\ Y = y + \omega$, ...
7
votes
1answer
96 views

Context-dependent functions

TL;DR warning! My question is not about getting the length of the ticks correct (although if someone has good suggestions, that would be welcomed). My question is about constructing functions that ...
1
vote
2answers
80 views

Avoid nesting of functions [closed]

is there a way to avoid the "nesting" of functions like f[f[f[a, b], c], d] where a, b, c, d are lists and f is a function that merges two lists with certain ...
0
votes
0answers
38 views

How can I get the covariance result cumulatively?

I am trying to derive the covariance results for n x 4 matrix where n is an arbitrary number. I am trying to derive the covariance for each row cumulatively. For example, I am trying to derive the ...
2
votes
2answers
142 views

Using list elements as function calls ?

Is it possible to take elements of a matrix or list and use them as arguments of a function? I have a matrix of values, something like this; ...
0
votes
1answer
59 views

Creating a symbolic function dynamically given an array of data

I have a function that is based on the numeric values of a random sample that I would like to maximize: ...
0
votes
0answers
38 views

NestList on a function where one of the arguments is chosen by the user

I need to NestList a function where the user chooses one of arguments. Here it is an example: ...
2
votes
2answers
191 views

How can I express this recurrence function in Mathematica?

\begin{equation} r_{i+1}=r_{i}+V_{i}{\Delta}t \end{equation} Where \begin{equation} V_{i}=(0.01, 0.40) \end{equation} and ${\Delta}t$ is the difference in time. My implementation (this is for ...
4
votes
1answer
125 views

How can I implement this recurrence equation in Mathematica?

I asked a similar question yesterday but today I realized that I was implementing the wrong function. I have tried changing the implementation but I can't get it to work. Problem: There is a river ...
4
votes
5answers
111 views

How can I convert a Piecewise into a nested If?

For later conversion into a programming language where if is the only conditional construct, I would like to convert a Piecewise ...
0
votes
1answer
46 views

How to make functions recall previously calculated results? [duplicate]

I need to calculate a complicated set of functions dpdv[j_], which depend on a set of variables m[n_]. They are time-consuming to calculate, and I don't know in advance which values I'll need. So ...
9
votes
3answers
166 views

Implement settable properties

I am looking for advice on implementing the following. (It is possible that the answer is that it is simply not a good idea to do this.) I would like to have an object similar to ...