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

0
votes
1answer
61 views

Using Derivative with list in curly brackets

I'd like to make a formula for a sum containing Derivative where the number of arguments in the latter depends on an input parameter n. Also involved is a list ...
1
vote
1answer
82 views

How to define an n-variate empirical distribution function probability for any n?

I'm using Mathematica 9.0 to calculate the probability according to the empirical distribution function (EDF) of some sample data. Afterwords this is included in a maximization stage so I define this ...
0
votes
2answers
165 views

Defining Piecewise Functions in Modules

In short, my question is the same as my previous one except that everything is now wrapped up in a module. The relevant code I'm working with is: ...
2
votes
0answers
79 views

Function that assigns properties [duplicate]

I want to create a function that creates and assigns properties to a symbol. In theory this would behave similarly to the NonzeroValues property of ...
1
vote
1answer
84 views

Defining a function with global parameters

What I would like to do in Mathematica is something like f[x_] := a x g[a_] := NIntegrate[f[x], {x, 0, 1}] But then of course I get ...
0
votes
1answer
72 views

Create jacobian Function from a vector Function

This is related to this question and maybe this question. I wanted a function that takes an n-parameter vector function and outputs a Jacobian matrix function, also n-parameter. I think this works: ...
5
votes
2answers
358 views

Define a function with a function for an input

I want to create the functions $even[f]$ and $odd[f]$ which will apply the formula $even[f(x)] = \frac{f(x)+f(-x)}{2}$ and $odd[f(x)]=\frac{f(x)-f(-x)}{2}$ for an arbitrary $f$. I tried using ...
3
votes
1answer
136 views

String pattern matching in function definition

I have a function f and its downvalues. I want to code an extra downvalue for arguments of the form "part1:part2" I don't understand why ...
0
votes
0answers
41 views

Using Table[ ] vs using Do[Append [ ] ] [duplicate]

Is it generally better/faster to use the Table function over using Do[Append []]? For example, I have this piece of code: ...
2
votes
1answer
322 views

Defining Piecewise Functions [duplicate]

I'm having trouble defining piecewise functions. What I have so far is a function that outputs something in a suitable format for Piecewise: ...
5
votes
3answers
504 views

How to define the Poisson bracket in Mathematica

If $f,g$ are functions of the independent variables $\{q_1, q_2, ..., q_N, p_1, p_2, ..., p_N\}$, then the Poisson bracket is defined as: \begin{equation*} [f, g] = \sum_{i=1}^N \left(\frac{\partial ...
9
votes
1answer
227 views

How to define function that can change patterns

Given time series data as follows: data = {{{2011, 1, 13}, 10}, {{2011, 2, 14}, 17}, {{2011, 3, 15}, 20}, {{2012, 1, 16}, 30}, {{2012, 2, 17}, 40}}; ...
0
votes
1answer
109 views

Holding Input Windows

I am creating a Mathematica document which contains a list of proofs in various fields of Mathematics so that I can easily review whenever I would like. First, I create a large set I call 'proofs' ...
0
votes
0answers
35 views

Varying number of function arguments [duplicate]

I have a function of six arguments: ffit[q1_, q2_, q3_, q4_, q5_, q6_] := (a1*q1^2 + a2*q2^2 + a3*q3^2 + a4*q4^2 + a5*q5^2 + a6*q6^2) ...
7
votes
3answers
157 views

Checking through an expression

I am writing a code that manipulates expressions that contains functions with an arbitrary number of arguments. An example expression is ...
3
votes
1answer
108 views

How to Override the Head of a Symbol

I was wondering if it is possible to override the head of symbols/operators. As a motivating example if I run: Then Ket will be returned. But lets say I want Bob to be returned instead of Ket. Is ...
1
vote
3answers
108 views

Difficult parametric function

I've got this strange parametric function and I want to implement it on Mathematica. I know that I could use Excel and calculate it, but my database is quite large and I want to do some reproducible ...
2
votes
2answers
175 views

Find partial derivatives when the number of variables is unknown

The idea is that I have a defined function of $n$ variables. I want to find out $n$ and get partial derivation for each of variables, Suppose you have a function ...
0
votes
1answer
168 views

Please explain this code (contains Append, Do, Coefficient)

I am a graduate student using a past student's Mathematica program. Will someone please explain to me exactly what this piece of code is doing? Specifically the ...
1
vote
1answer
47 views

How to substitue local variables into expression with free variables?

I'm wondering how to do the following: expr = L + R; foo[L_, R_] := expr; foo[1, 2] (* L + R *) and have it substitute the function variables ...
-1
votes
1answer
74 views

How to specify a specific range for a loop?

I'm having trouble with setting up a particular range in a loop for my programming. Now I have a defined range of data (not continuous numbers) that needs to be applied in a loop. Since the aim for my ...
1
vote
2answers
316 views

Define function using variable list

I have a list of variables variables={a,b} and a list with values values={1,2}. I'd like to define a function using the ...
4
votes
2answers
326 views

Can Mathematica functions have static variables?

In C, one can have variables that are declared static. These are not re-initialized every single time a function is called. ...
1
vote
1answer
95 views

Possibility of evaluating the expression within a NestList at each iteration?

I want to rotate iteratively a triangle about the same point, increasing for each iteration the length of its side in the same proportion and change the color of its sides. I came up with the solution ...
4
votes
1answer
113 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: ...
20
votes
3answers
385 views

Why x = x doesn't cause an infinite loop, but f[x_] := f[x] does?

If I execute: In[1] := x = x Out[1] = x and then I evaluate the symbol x: ...
1
vote
5answers
95 views

Question about sum and list in a function

I have a list of this form: $ lst =\left\{\left\{q_1,x_1,y_1,z_1\right\},\left\{q_2,x_2,y_2,z_2\right\},\text{...},\left\{q_n,x_n,y_n,z_n\right\}\right\} $ I want to use this list in the function: ...
4
votes
1answer
182 views

What does it mean when a function is defined with two pairs of brackets

I have noticed a function defined as such: f[x_][y_] := expr; How is the argument y in 2nd pair of brackets being used? What is the difference from defining a ...
2
votes
3answers
147 views

How to generate a series of bisection points of a given line segment?

I want to get the coordinates of the bisection points for a given line segment to a specified resolution. For example, when the line is given by {0,0} and {1,0}, the output coordinates should be ...
0
votes
1answer
139 views

A function generating particle-in-a-box eigenfunctions

The function sequence is defined over $[-\frac{L}{2},\frac{L}{2}]$ (all elements are valued 0 outside this interval): $$\psi_n(x)=\begin{cases}\sqrt{2/L}\sin{(\frac{n\pi x}{L})} & n \space even ...
1
vote
3answers
184 views

professionalize/optimize my code that calls a function

I have the following construction, which defines a function that I subsequently call in a loop multiple times. I use this very often, but I have never looked into if there is a more professional way ...
1
vote
1answer
117 views

Module that finds the sum of digits of any given positive integer

How can I make a module that finds the sum of digits of any given positive integer? I use this command F[x_]:= Apply[ Plus, IntegerDigits[x] ] Is there another ...
1
vote
1answer
208 views

Part::pspec error returned when using a ListLinePlot ColorFunction variable as list index

In ListLinePlot, I'm attempting to construct my own ColorFunction for coloring points differently based on a third dimension of data not plotted to a specific axis. However, ColorFunction only uses ...
2
votes
2answers
83 views

`PrimeNu` counting function

Building on this question, what is the most efficient counting function for distinct prime factors? It would obviously be more efficient if Prime and ...
4
votes
2answers
180 views

Is it possible to `Protect` a dynamic programming process?

I wrote a function using dynamic programming to speed up the evaluation. For safety, I also want to protect this function. Take factorial for example: ...
2
votes
1answer
81 views

Need to run evaluation 2 times

Why does this only work after running the evaluation 2 times? ...
8
votes
2answers
246 views

Automatically compute r[1] = r1; r[2] = r2; …

I have three variables like this, r1 = 4; r2 = 3; r3 = 1; and I want to insert them into a function use them in a function definition like so: ...
1
vote
1answer
97 views

Linearity of a function in Mathematica

I have a function which has something like myFunc[q,a state[c,d]] a could be anything, and I want to tell Mathematica that ...
5
votes
2answers
264 views

Manipulation of InterpolatingFunction

Suppose that $q[t]$ is obtained by NDSolve as an InterpolatingFunction, and I want to define $Q[t]$ to be some function of ...
3
votes
3answers
237 views

Prime factor counting function

Is there any way I can speed up this prime factor counting function? (I am looking for all numbers in a range with 3 prime factors (counted with multiplicity).) ...
1
vote
3answers
183 views

Pure function inside another pure function

I have the following working expression: In[15]:= Length[Select[IntegerPartitions[10],First[#1]==5&]] Out[15]= 7 But, instead of using the constant 5 I want ...
4
votes
3answers
1k views

Make a differentiable smooth sawtooth waveform

So, I am trying to understand the effect of sawtooth waveform on a mechanical system model. For example, a system of a plate and a box on it. And then we try to oscillate the plate. In order to do ...
8
votes
3answers
202 views
3
votes
0answers
65 views

How to combine DifferenceRoot objects for odd and even-indexed terms

I'm trying to analyze a certain infinite sequence $S$, indexed by positive integers starting from $1$. It can be split into two subsequences: $S^{odd}$ by removing all even-indexed elements and ...
1
vote
2answers
197 views
0
votes
2answers
167 views

How to insert trigonometric functions into matrices' vector components? [closed]

Is it possible to insert a trigonometric function into a matrix which will be able to be used dynamically, as an element of one of the vectors of the matrix? I'm trying to perform an operation like ...
-4
votes
1answer
112 views

Programming Multipolar Expansions in Spherical Tensors

We have that a density matrix can be written in a basis of spherical tensors: $\rho =\sum _{K=0}^S\sum_{q=-K}^K \rho _{\text{Kq}}^{(S)} T_{\text{Kq}}^{(S)}$ for example for a 3x3 matrix, $S=1$, ...
0
votes
1answer
106 views

Is it possible to define a function in terms of a power series?

Is it at all possible to define a function in Mathematica in terms of a power series? For a specific example, I wish to write $x(\tau)$ as $x(\tau)=x_{0}(\tau)+\mu ...
1
vote
1answer
129 views

Orthogonalizing polynomials with inner product depending on parameters

I need to orthogonalize the polynomials $h_n(x)=x^{2n}(1+x^2)^{-4S}$ with $x\in\textbf{R}$, $2S\in\textbf{N}$ and $n\in\{1,3,5,\ldots, 4S-1\}$ over the inner product $\langle h_n,h_m\rangle=32\pi ...
5
votes
1answer
66 views

How to bail out from a non-applicable transformation function?

I provide a custom function in TransformationFunctions options of FullSimplify that can do a transformation only for some values ...