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

16
votes
3answers
271 views

Is it possible to change the order that symbol definitions are checked? (DownValues vs UpValues)

Suppose I have the function f with the following definition assigned to it: ...
6
votes
2answers
272 views

What evaluation mechanism is behind Mathematica support for multiple definitions of a function? What is the benefit of this?

I am studying the question What are the most common pitfalls awaiting new users? Up to now, I have fulfilled some of the parts but yet don't understand which this problem is related to? Consider the ...
10
votes
1answer
231 views

Question about Rules and Pattern (The operator patt/;test)

Maybe this question is so trivial but it has confused me. I'm studying the What the @#%^&*?! do all those funny signs mean? and in the Rules and patterns under ...
0
votes
1answer
87 views

Optimization of a voxel projection function

I implemented a function to check if a voxel (3D pixel) is well projected on the space of a image (black and white silhouette) through a perspective camera. The function does the work but It need more ...
2
votes
3answers
168 views

data manipulation in log-log scale

Say that I have the following: y[t_] := 2 + (2/10)*(1 - Exp[-t/11]) timedat = N[Table[{t, y[t]}, {t, 0, 10, 1/1000}]]; How is it possible to construct an ...
4
votes
2answers
105 views

Unsure how to nest / fold this power tower factorisation

Not quite sure how to nest this. What I would like is power tower of integer factored until all powers are primes. eg desired output for f[3*125^4] would be ...
0
votes
1answer
70 views

a function that evaluates a Section

I have defined a function that returns different output according to the values of the axes of an ellipsoidal. The function is given below: ...
2
votes
0answers
36 views

Unexpected behavior from Return called in a Block [duplicate]

I like to write functions that dispose of failure cases early, before they do the main calculation, but I don't like to write cascading If statements. Here is a toy ...
1
vote
1answer
63 views

Writing a function with two functions as arguments

I am trying to write a function of two functions. A function of one function works as in h[f___, x_, y_] := Integrate[f, x] h[x^2 + y, x, y] and this ...
2
votes
3answers
87 views

Making a list of functions

Suppose I have the following equation system to solve: x(t_):={x1(t),x2(t)}; DSolve[x'(t)==x(t),x(t),t] This code works fine, but now suppose that instead of 2 ...
9
votes
1answer
176 views

Finding large prime gaps

I am using basic searches for prime gaps as follows: ...
12
votes
4answers
208 views

Distributing function arguments with function compositions. How to compute $(f + g^2)(x) = f(x) + g(x)^2$?

Suppose I have two functions f and g. To compute $f + g$ evaluated at a point $x$, I know that one can use ...
0
votes
1answer
70 views

Working with functionals [closed]

I am trying to work with functionals in Mathematica, this means that I want to define a function of a function. I defined my function of a function as: ...
1
vote
2answers
43 views

Replace function with multiple functions while respecting the inputs

I want to replace a function with either one or two functions and also combine their arguments. Say I have an original expression expr = u[i + 1] r[i] r[i-1] ...
3
votes
5answers
305 views

Function involving For loop

I've written the following function (the code includes the output): ...
1
vote
1answer
91 views
17
votes
3answers
466 views

Improving speed of code computing number of nonrepeating partitions

I need to answer the following for a number of parameters: How many ways can the integer $k$ be written as a sum of $n$ different integers ranging from $1$ to $m$? My initial attempt was the ...
0
votes
0answers
56 views

Is there a IsDefinedQ function? [duplicate]

I have a function which is a bit expensive to calculate. I want to translate and integrate. I want to translate by values on a grid, store the values of the integral and refine the grid. That is, I ...
2
votes
1answer
63 views

How can an argument of the form f[t] be injected into the RHS of a function definition [duplicate]

This is what I want, for an arbitrary function x This is what I have done: ...
5
votes
2answers
133 views

How to specify and use nested options for a function?

I want to write a function that takes options that are nested. For example, suppose the function has one main option, "method" which can take two values, "a" and "b". Also, when method is "a", it can ...
4
votes
2answers
233 views

Generate the Fibonacci sequence with Accumulate

Is there a way to generate the Fibonacci sequence with the Accumulate function?
6
votes
2answers
105 views

Force evaluation of the right-hand side of a local variable definition

One can do expression = a + 1; f[a_] := Evaluate[expression] but how would one do something like this ...
-1
votes
4answers
104 views

Converting an expression to function [duplicate]

I have the following problem. I current can automatically create the variable positionPayload, for instance: ...
0
votes
1answer
89 views

Replacing the Slot in an expression

So I've got Mathematica to handle the grunt work for me to obtain expression: Root[ (* blah, blah *) + #^2 + (* etc, etc *) ] Now, the value inside Root is a ...
4
votes
1answer
106 views

Substitute a function's definition for its name

I'm having trouble with a function which is supposed to work on another function. Simplified version: ...
3
votes
2answers
69 views

How can I adjust the global input variable of a function to the value of a local variable within the function

I am trying to define a function f[l_] that will perform an operation on whatever input variable I use (normally I use l which ...
1
vote
2answers
50 views

How to define a function `deriv[f[x]]` such that its output is an unevaluated expression `fx=D[f[x],x]`?

I want to define a function deriv[f_[x_]] such that when I run deriv[f[x]], the output is an unevaluated expression ...
0
votes
2answers
58 views

Function to list Collatz sequence doesn`t work [duplicate]

I am new to Mathematica and was trying to make a function that lists the Collatz sequence stating with a given number, but I can´t get it to work. Here is my function: ...
1
vote
1answer
114 views

Defining a derivative on a custom function

I am trying to define a derivative that acts on a custom function, $f$, such that when it acts, it increments one of the arguments of by 1. f is not defined; I just want to use it to keep track of ...
2
votes
1answer
62 views

Dropping rational coefficients

I have a list of expressions (they all are exact numeric quantities, not containing any variables), some of them have integer or rationals coefficients, or complex coefficients with rational or ...
1
vote
3answers
121 views

Setting ::usage for variables returned from a function

What is the best way to achieve {v,u} = f[]; (* Returns value and usage description *) v::usage = u; from inside f, i.e., ...
2
votes
1answer
60 views

Manipulating unevaluated expressions in nested function calls

This is a follow-up to my earlier question: Passing an unevaluated part of an association to a function I'm trying to create a modular dashboard that controls sound volume of an audio system ...
0
votes
1answer
102 views

Element assignment in matrix function

I have to define a function of two variables that yields a high dimensional matrix, with each entry containing a scalar function, that uses its coordinates in the matrix as input. like this: ...
2
votes
1answer
153 views

How to get numerical results from HeavisideTheta?

I have defined a table tab = {7.52521, 17.0083, 22.5678, 11.8707, 8.27799} and a function ...
1
vote
1answer
84 views

Min function for functions f1, f2, [closed]

There are several "min" functions in Mathematica. In the "Numerical" category, there is a "Min" function that works on a numerical list. Then there's MinValue for finding the minimum of a whole ...
0
votes
0answers
38 views

Dot production associated with comparison process of different matrices eigenvectors

With thanks to Guess who in the link module using.. We created functional 5*5 matrices: ...
3
votes
1answer
144 views

How can you get the area of a region from an image?

Suppose we have a square of side "L" m, How can I calculate the hatched area, if the corners of the square are centers of circles. It can be generalized to any region? edit: This is the result I ...
22
votes
4answers
311 views

Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)?

For various reasons related to workflow associated with creating figures for journals, I am creating functions that will accept graphics primitives and a set of options and spit out a ...
2
votes
1answer
51 views

Appending Delayed Evaluation

I have a function that need to be summed over all parameters given in a separate list. f[x_,y_,z_,n_,i_]:= (*Some function*); list={{n1,i1},{n2,i2},...}; I need ...
2
votes
2answers
172 views

Multiply pure function with scalar

Somtimes I still get lost in the Hold, HoldAll etc. functions of Mathematica. I want to scale a simple PDF function with a previously calculated variable e.g. ...
1
vote
0answers
59 views

function of a function [closed]

I have the following two functions, 'f' and 's': ...
0
votes
1answer
50 views

Finding function by constraints

I have the following coefficient finding problem: f[x] := a * Log[b*x + c] + d Constraints: f[0] = 1 f[80] = 0.5 I want to find the coefficients of f that ...
0
votes
1answer
53 views

Mapping Linear Model Fit Over An Arbitrary Number of sets

I have multiple sets of data, and I want to apply Linear Model Fit sequentially to each set of data. For example, below tstlst contains 3 data sets. Right now I ...
3
votes
2answers
153 views

How to declare a function as real in order to get rid of Conjugate in front of the expressions?

I have a complicated complex function containing some other ones that are real, f=f(A,B,C,...) ,where A=A(r) and it is real, for ...
2
votes
2answers
71 views

Plot does not take range with OptionValue and PlotRange option

I am making a function that is to use Show to overlay a few plots of a function. I want the first Plot in ...
1
vote
0answers
62 views

creation a table from a default module [closed]

I have used of bisection method for finding the root of a function: ...
7
votes
5answers
250 views

How to write an operator that acts on pure function (to give another pure function)

I would like to write an operator oper that acts on a pure function f with an undetermined number of arguments, with the syntax ...
6
votes
1answer
125 views

Is there an elegant way of delaying evaluation of `A==B` when calling a function that uses this pattern

Suppose I wanted to define a number of functions that accepts expressions of the form: ...
1
vote
1answer
66 views

Functions becoming undefined when evaluated

I'm so confused I don't even know how to phrase my Question title. My function nextCol works until I use colLength, then ...