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
39 views

Summimg peculiar nestings

I have some lists with peculiar nestings: ...
1
vote
1answer
51 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: ...
0
votes
2answers
24 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
123 views

Localising OptionValues

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

Testing for a vector of reals [on hold]

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

Numerical Integration/Function

I have a problem with a function defined numerically through an integral of the form: ...
1
vote
1answer
102 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
38 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
43 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
92 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
92 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
85 views

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

Assming that I have a function myFunc which has some options. ...
6
votes
4answers
216 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
66 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
62 views
1
vote
0answers
61 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
77 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
65 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
61 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
52 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
143 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] ...
1
vote
1answer
107 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
37 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
170 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 ...
0
votes
0answers
75 views

How to conveniently stop a programme without aborting the computation

I have a programme that will have to run for a few days. However, I would like the user to have the option of stopping the computation of the code, without aborting it, when he desires, without ...
1
vote
1answer
100 views

Quantize a real-valued function?

I'm wondering if there's a built-in way in Mathematica to take a function whose output values are continuous, and quantize it to produce a step function. For example, I have the function ...
8
votes
1answer
118 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
0
votes
1answer
53 views

Defining a function with algebraic parameters

I am trying to define and use a function to represent the eigenstates of a Quantum Harmonic Oscillator, which have the general form: $$\psi_{n}(x) = \frac{1}{\sqrt{2^{n} n!}}\left(\frac{m \omega}{2 ...
0
votes
0answers
55 views

When to break large programmes into smaller programmes?

Say I have a main function ...
1
vote
1answer
93 views

Why doesn't Thread work in this case?

The following function is supposed to pick the element of the list with highest weight, i.e., with Thread [f[{{a,1},{b,2}}]] it should make the variables ...
3
votes
1answer
104 views

Define and plot a piecewise function with parameters [closed]

I have this function , but i do not know how to define it in mathematica \begin{equation} f_t(x)=% \begin{cases} i& \text{$\frac{1}{1+i}<|x-t|\le \frac{1}{i}$} \\ ...
2
votes
3answers
79 views

How to test a condition for a list and output all the expression that is true?

Suppose that I have a list lis={x-y,x+y,x*y}; and a function f[a_,b_]:=#/.{x->a,y->b}& I want to output all the ...
1
vote
1answer
46 views

Composition of Multivariable function

I am trying to compose functions of xleft, maybe like xleft[xleft[f]] or even higher order. And here is my function: ...
2
votes
1answer
248 views

How to make a function from body with variable number of arguments?

Suppose I have an UNKNOWN function, which generates formulas with given number of x[i]. ...
0
votes
0answers
45 views

Question about treillis structures

I have difficulties finding a command to plot multiple segments connected in nodes in Mathematica. Ultimately, my goal is to modelise something like this by entering nodes coordinates. Also, the ...
4
votes
2answers
68 views

Count the number of element repeats at various distances

I have a function of x: f[x_] := Abs[Extract[list, x + n] - Extract[list, x]] I then apply this: ...
6
votes
3answers
187 views

How to define a nested function like this?

For example, I want to calculate the limit of $\frac{1}{1+\frac{2}{1+\frac{3}{1+\ldots}}}$. So I want to define: $f(1)=1$ $f(2)=\frac{1}{1+2}$ $f(3)=\frac{1}{1+\frac{2}{1+3}}$ Then I calculate ...
2
votes
2answers
119 views

Understanding `NestList` with multiple functions

General question For a function f[n, a], where a is a list and n is a whole number, what is ...
2
votes
2answers
103 views

Can't use my own discontinuous coefficient within NDSolve

I want to evaluate the pendulum's motion with changing equilibrium state given by eqilValue[t]: ...
6
votes
2answers
158 views

Using a held option

Woud there be an elegant way to use a held option ? For example if I need to pass by reference several variables but I don't always need all of them, it can be useful. For example if I want to append ...
0
votes
1answer
82 views

Recursive functions taking list arguments

I wish to create a function that takes lists (e.g. {plus,plus,minus,0,minus,plus}) and uses the input list to calculate a corresponding value using a set of ...
1
vote
1answer
75 views

Help creating a simple function

Can someone help me to create a function. The truth I'm new in Mathematica and I don't understand well the way functions work in I. I need a function that animate some rectangles according to a curve ...
2
votes
1answer
34 views

Linear function on strings using UpValues

I have two functions Sup and Sdown that take a string of letters with allowed characters ...
0
votes
1answer
108 views

Optimizing Chi-square distance between two distribution

I want to program the chi-square between two distribution Xu and Vu with length N. This function is defined as below: Xu and Vu are real and positive. ...
1
vote
2answers
51 views

How to define a function that is related to derivative of Jacobi theta function

I would like to make 3D plot of the following function. ...
3
votes
1answer
62 views

Defining a function that prints both the name and value of a variable defined locally within the calling function

To save time when debugging with Print statements, I'd like to define a function -- call it PrintVal -- that prints both the name and value of a variable defined locally within the calling function. ...