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

1
vote
1answer
46 views

Function taking arbitrary number of arguments

I have a very similar function to that which a solution was provided by kguler, but am unsure as to how to adapt the solution that was given there. That post can be seen here. The adaptation is only ...
1
vote
0answers
69 views

Find algorithm or generator formula with automated procedure and pattern recognition

I am trying to find a generator formula for the discriminant of a polynomial and believe it would be an interesting problem for the Mathematica community to know how to automate evaluation of certain ...
3
votes
1answer
110 views

How can I overload a function with multiple bracket-slots so f[a][b] and f[a] can coexist?

Maybe this is not even possible: I want to create a function f that can have two input brackets like: f[a_][b_:1]:= a*b and ...
1
vote
1answer
49 views

NestList and RandomChoice from a list of functions

I am tryingg to modify Chaos Game Fractals from Wolfram Demonstrations. The part I am having problem with is this. Original code: ...
1
vote
3answers
107 views

IntegerQ returning False despite setting Assumptions

Why does the following not return True? Assuming[z ∈ Integers, IntegerQ[z]] False I tried this on a fresh session of ...
6
votes
1answer
195 views

How can a function know in which function it was evaluated?

Hello my second question here might be very hard. I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function ...
1
vote
3answers
168 views

Defining functions when there are a lot of parameters

Suppose I have two lists of parameters: $\delta=\{\delta1, \delta2, …\}$ and $\gamma = \{\gamma1, \gamma2, …\}$. The question is: how can one use $\delta$ and $\gamma$ (the parameter vectors) within ...
0
votes
2answers
60 views

Supply a function as an argument to another, and find its minimum

I would like to minimize a function, which is supplied as an argument to another function, and report back the estimates of the values of the variables that result in its minimum. In specific, I would ...
2
votes
2answers
56 views

function definition with a given list of parameters: how to use Evaluate[] properly?

given a list of variables variables = {a, b, c}; I want to automatically define a function f[{a_,b_,c_},x_] := g[a,b,c,x] ...
1
vote
1answer
143 views

General function taking general number of arguments

The following is an edit of this post, for which a working solution was provided by John McGee. I have a different function and am not sure how to adapt the solution for it: I want to construct a ...
0
votes
1answer
47 views

Defining functions of varying, iterative numbers of arguments

The following is an edit of this post, for which a working solution was provided by John McGee. I have a different function and am not sure how to adapt the solution for it: I want to construct a ...
0
votes
0answers
29 views

How to evaluate NMinimize inside a function

I want to define a function that returns a sum of differences of some points to a given function. To obtain these differences I would like to use NMinimize, but if I implement that straightforwardly, ...
5
votes
2answers
215 views

Optimising coefficient finding functions

I'm seeking a generalisation to this question, where I am looking to list the coefficients (without repetition) to monomials of the form ...
0
votes
0answers
30 views

Remove list head to insert as arguement [duplicate]

I'm trying to use a set of terms, e.g., {x,y,z}, as an argument for another function with doesn't take in a list but a series of variables (e.g. need ...
0
votes
1answer
45 views

Semi-delayed assignment: analytic now; numeric later

The problem starts with a medium-complicated function, f[y0_,y1_,y2_,s_,d_], with everything Real. The innards of this function don’t matter here, except to say that it is analytically differentiable. ...
0
votes
0answers
33 views

Doing a repeated calculation on unique chunks of data based on a value in a column

First post ever on a forum, please bear with me if I don't get it right the first time :) I'm making a script for someone that imports a data file, generates some histograms, tables, and outputs them ...
1
vote
1answer
48 views

How to modify the output result of an evaluation

For ease of notation, I have defined a function, $f$, that defines the permutation of Bosonic particles such that it has the following properties (the form of $f$ is irrelavent for this question): ...
7
votes
3answers
214 views

Why these ways of defining a function with variable number of arguments fail?

First: I know about the correct way to define a function with variable number of arguments: Clear[f] f[s__] := Plus[s] f[1, 2, 3, 4] (* 10 *) BUT I ...
4
votes
2answers
129 views

Nested mapping of a function

If I use the construct f[#] & /@ f[#] & /@ f[#] & /@ {a, b, c} my function works, but if I use ...
0
votes
1answer
51 views

Question about output of a function

I'm pretty new to programming in Mathematica and have a question. I wrote this function to factorise large numbers. The outputs are almost as expected, but I don't understand where the null's come ...
3
votes
5answers
246 views

Define a function with variables linked implicitly

I would like to define a function $f(x,y)$, with the variables x and y linked by an equation implicitly, so $f(x,y)=f(x(y),y)=f(y)$. Eventually I would like to draw a graph with $f(x(y),y)$ being the ...
3
votes
2answers
183 views

Improve timings for calculating coeficients

Is there a quicker way of finding ther coefficients of something like $$(x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2}+x_{5}^{2}+x_{6}^{2}+x_{7}^{2}+x_{8}^{2}+x_{9}^{2}+x_{10}^{2})\times\\ ...
10
votes
1answer
168 views

Copy Built-in function to new name

It is know that some built-in functions are system function and changing their properties may cause trouble. Suppose for example I want to change features of Cases: ...
1
vote
1answer
48 views

Mathematica defines a function as list

This might be a very silly question but i'm defining a function as: \[Chi][AR_] := ((AR^(2) - 1)/(AR^(2) + 1)); which is clearly a scalar function of AR; however ...
0
votes
2answers
39 views

Custom function call does not return a value

I have written the following code to implement a PCA algorithm. It works on standalone basis, however when I put it into a function that is called the following ...
3
votes
1answer
157 views

Creating the Nabla operator as a operator

How can I define the nabla-operator as a an operator, acting on everything to the right of the operator! Also taking \[Del]^2 would give the second derivivates. I wish to be able to write ...
0
votes
2answers
79 views

How do I define the following piecewise function?

For 0<=t<=10, if 5t<=n<=5t+2, then f[n]=4n, and if 5t+3<=n<=5t+4, then f[n]=4n+1? I would like then to use the specific values of f[n], for all n between 0 and 54 (t here is any ...
7
votes
2answers
288 views

Using up-values and down-values

I have a problem I'm trying hard to find a solution for, although I don't know if a solution exists. The following rules produce the result e[2].f[] ...
1
vote
2answers
92 views

Theory => practice: constructing a function “functionally”

I've read the Mathematica documentation and am certain there's way to do this with functional programming, but can't quite conceive yet how. I've taken a simple example (stating the series for ...
3
votes
1answer
106 views

Given a list made up of subsets of an alphabet, how can I use associations to create a Boolean dictionary of the given list

I have a list of 30,000 elements, each of the element is a list in itself {{a, b}, {p, q, r}, {a, p}, ..., {z, x}}. Second one contains unique elements ...
2
votes
1answer
94 views

Table of references [duplicate]

I have a list of variables (say a, b, c, d, e) that I want to assign in a single command. {a,b,c,d,e} = {1,2,3,4,5} would ...
1
vote
1answer
47 views

Summimg peculiar nestings

I have some lists with peculiar nestings: ...
1
vote
1answer
85 views

How in Mathematica, would I define a function that takes arguments much like one would in an object orientated language

I would know how to write a function in object orientated language, just I don't know how to do this in mathematica, in an OOP I would define a function like this: ...
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
3answers
161 views

Localising OptionValues

The documentation on OptionsPattern leaves me with some unanswered questions. From the example section: ...
2
votes
3answers
95 views

Testing for a vector of reals [closed]

Suppose I define: RealVector[expr_]:=VectorQ[expr,NumberQ[#]&&Head[#]===Real)]; Then: ...
0
votes
0answers
49 views

How to Fold (FoldList) this in a more compact (efficient) way?

Example code: x = Table[something[i], {i, 5}] Table[Fold[f1[#1, f2[#2, a], a] &, x[[i]], {c1, c2, c3}], {i, Length[x]}] This above is just an example. I ...
2
votes
2answers
161 views

Polynomial expansion of operator

I am new to Mathematica, I am trying to generate the polynomial function of a operator. So for example, the operator $L $ is $\frac{\partial f}{\partial x}+\frac{\partial f}{\partial y} $, and I want ...
0
votes
0answers
81 views

Numerical Integration/Function

I have a problem with a function defined numerically through an integral of the form: ...
1
vote
1answer
110 views

Why does Mathematica add Hold to the result of my user defined symbol?

In the code below I have defined a symbol that multiplies two block-matrices (I can't use the builtin Dot since Dot assumes that the matrix coefficients commute): ...
0
votes
1answer
88 views

Defining solutions to FindRoot as a function of parameters in Mathematica

This works: f[a_] := x /. FindRoot[x^2 - a == 0, {x, 1}] f[3] (* 1.73205 *) I need this to work, but it doesn't: ...
1
vote
0answers
54 views

How to input a dynamically changing parameter in NDSolve? [closed]

I am trying to solve a system of DEs with additional conditions, and one condition depends on a parameter. Let us consider the following example: ...
4
votes
1answer
106 views

Display level of recursion of a recursive function during execution

Given a recursive function, is there some simple way to display or store the value of recursion or "how many levels deep" the function is at a given time? For instance, if we wrote the recursive ...
2
votes
1answer
328 views

Inverse gradient operator

I found a nice paper about inverse vector operators here. I have successfully implemented a Mathematica function for most of them, however I can't figure out how to do inverse gradient (page 7 in the ...
1
vote
1answer
101 views

How to deal with the condition that a function own many options?

Assming that I have a function myFunc which has some options. ...
7
votes
4answers
251 views

Function for a series of joined slopes

I need a function for a series of joined slopes and my solution feels a bit kludgy. Is there a better way? A list of pairs of transition points and slopes: ...
3
votes
2answers
71 views

Forward function arguments to built-in Plot[]

Suppose I need to write a function to plot the real and imaginary part of some function (just an example). I want to define a function foo so that ...
0
votes
1answer
79 views