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
135 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
45 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
211 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
46 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
211 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
127 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
239 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
180 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
166 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
135 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
77 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
278 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
98 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
90 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
46 views

Summimg peculiar nestings

I have some lists with peculiar nestings: ...
1
vote
1answer
79 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
159 views

Localising OptionValues

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

Testing for a vector of reals [closed]

Suppose I define: RealVector[expr_]:=VectorQ[expr,NumberQ[#]&&Head[#]===Real)]; Then: ...
0
votes
0answers
48 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
149 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
78 views

Numerical Integration/Function

I have a problem with a function defined numerically through an integral of the form: ...
1
vote
1answer
107 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
70 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
52 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
103 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
236 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
96 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
246 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
69 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
71 views
1
vote
1answer
87 views

The problem (time) caused by ?NumericQ

In here, I asked a related (similar) question. Using ?NumericQ seems have slowed down the entire calculation. But I now have a more serious problem: ...
1
vote
2answers
99 views

Expressions containing globally undefined symbols inside a function where they are defined

Consider the following code. f[a_,b_]:=x; Clear[a,b]; x=a+b; f[1,2] It returns a+b. However, from a certain viewpoint, one ...
1
vote
0answers
69 views

What are the differences between these functions when used for NMaximize?

In short, the question is how to construct the function you want to optimize? ...
0
votes
0answers
67 views

How to define a two-variable sequence?

I would like to define the following function but I am having a problem. I would like Mathematica to "understand" what I mean by writing "+...+". For example, for large values for $n$ and $m$ it takes ...
2
votes
1answer
56 views

How to protect function variables [duplicate]

I am sure this has been asked before, but I can't seem to find the right solution. I want a function to be the solution to a differential equation ...
3
votes
1answer
151 views

Function that returns the second argument of Reap

I'm trying to create a function that returns only the second argument of Reap. Here's my naive guess of how it would go : Reap2[x_]:= Flatten[Reap[x][[2]],1] ...
2
votes
1answer
174 views

Piecewise smooth rectangle pulse

I am wondering how to create a single rectangle pulse with nonlinear riding and falling edges on both sides. I don't know whether it is better to do it piecewise or to mathematically transform the ...
3
votes
1answer
39 views

How shortening argument test when declaring functions?

When defining some functions which depend in many arguments sometimes we need to include question Q constraints to diminish processing time. My question is simple, there is a way to shorten a long ...
4
votes
2answers
178 views

A better way of handling Module output?

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of ...