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
5answers
109 views

Converting multi variable expression into a function

So I have this rather complicated looking expression ...
3
votes
2answers
419 views

Using Functions as Arguments to Function

I am trying to make a function like Norm but defined like so: MyNorm[{x_,y_,z_}]=Sqrt[x^2+y^2+z^2]. Mathematica assumes that x, ...
17
votes
2answers
456 views

Using several anonymous functions mixed together

This works: list // BarChart[#[[2]], ChartLabels -> DateString @@@ #[[1]], ChartStyle -> "Pastel"] & This doesn't: ...
6
votes
1answer
217 views

Compiling a function that takes lots of optional arguments

I have a function that takes lots of optional arguments (basically some design parameters, about 10 of them) and performs some basic algebraic calculations involving complex numbers with the options. ...
1
vote
2answers
57 views

How to generate commands which are very similar?

I have several data sets that I wish to interpolate and integrate like this: ...
15
votes
2answers
429 views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
32
votes
5answers
1k 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 ...
1
vote
2answers
73 views

Need help with code for number theory problem

I'm completely new to Mathematica (used previously only for very simple cases). I need to write a quite complex function. The function must do the following: Input consists of two numbers: a and b. ...
2
votes
1answer
73 views

Generating list of functions from list of expressions

I know how to create a function from concrete expression like Function[{x, y}, x^2 + y^2] But what if I have a list of expressions list1 and I know the set of ...
3
votes
0answers
58 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
62 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
128 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
56 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 ...
50
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
181 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 ...
6
votes
1answer
170 views

Two identical functions give different answers

I am new to mathematica and I hope this is a simple problem. I have two identical functions that only differ by the variable names: ...
3
votes
2answers
64 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
86 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: ...
1
vote
1answer
99 views

Changing default behavior of Series to give slightly different SeriesData

I am working with complicated expressions expr that contains the symbolic function f[x] which I know to have a pole at ...
2
votes
2answers
69 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 ...
28
votes
1answer
609 views

How to augment the realm of functions Mathematica thinks it knows how to integrate symbolically

My question involves extending the functionality of Integrate over specific integrals in the most generic manner. Specifically, is it possible to "hack into" ...
1
vote
2answers
50 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
82 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
58 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
34 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
44 views
4
votes
6answers
392 views
5
votes
1answer
82 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
38 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
115 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
83 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
75 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
54 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. ...
23
votes
3answers
1k views

Using pure functions in Table

I need a table with the elements made of pure functions and list elements. This is a simplified example: I need a list as: ...
1
vote
2answers
62 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 ...
26
votes
4answers
1k views

Can a function be made to accept a variable amount of inputs?

I have a function that takes two inputs and processes them for a single output. What I need is one that can take a varying number of inputs. and process them to a single output. Is this possible in ...
0
votes
0answers
37 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
139 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
75 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
24 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
22 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
45 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
144 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
47 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
77 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: ...
4
votes
1answer
117 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 ...