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

4
votes
1answer
189 views

Find lengths of contours in a ContourPlot

I am trying to find the lengths of different contours in the following plot: It is a complicated piecewise function evaluated on the unit disk. I am hoping there is an easy, generalized way to ...
4
votes
1answer
173 views

Feedback on my Subscript code

I have written a code, and I need a little feed back.. I want to make the Subscript distributive across (1) addition and (2) multiplication to mimic short-hand for ...
4
votes
1answer
233 views
4
votes
2answers
63 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: ...
4
votes
2answers
88 views

Function pattern to support listed and non-listed arguments [duplicate]

I often want to write functions that take as an argument either a) a rule or b) a list of rules. As an example, when using Replace with just one rule, it does not ...
4
votes
1answer
176 views

Finding mappings between expressions

Suppose we have an expression of the form: $j=\frac{A\left(t\right)}{B\left(t\right)}=\frac{C\left(s\right)}{D\left(s\right)}$ That is, $j$ can be expressed either as a function of $t$, or as a ...
4
votes
1answer
203 views

What's the most intelligent way to store the information of a selfmade graph?

I have a very long list of element, say $a1,a2, a3$,..., and they are all connected in that each is the child of many parents. E.g. $a74$ is associated with $a2,a55,a71$, and also contains ...
3
votes
9answers
2k views

How to find palindromic numbers (Project Euler #4)?

I'm trying to solve the fourth Project Euler's problem with this: ...
3
votes
3answers
349 views

Alternatives pattern in a function definition

I thought that I remembered a way to write a function definition using "|", but can't find documentation for it. So for example, I'd want to define: ...
3
votes
3answers
375 views

How to pull scalars out of a function that should act on lists?

Suppose I have A = a vecA B = b vecB where a and b are supposed to be arbitrary scalars ...
3
votes
2answers
445 views

Defining a function using Series? [duplicate]

Possible Duplicate: General::ivar is not a valid variable when plotting - what actually causes this and how to avoid it? I need to define a function using the output of series, i.e. ...
3
votes
4answers
222 views

How to create a composite function from a variable number of components?

Background of the problem: A patient takes a drug every $X$ hours with a half-life of $Y$ hours. How much of the drug will be (exactly) in his system after $h$ hours? For $X=24$, and $Y=24$ I made the ...
3
votes
4answers
164 views

Pack Solve results into a vector

I am currently using a really easy function to get the eigenvectors of a corresponding eigenspace: ...
3
votes
2answers
101 views

How to define multiple functions using a for command

Let's say I want to plot 10 graphs of those following functions. Since they are recursive functions, I cant make a table with 10 different initial conditions because Mathematica stores each value ...
3
votes
3answers
242 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).) ...
3
votes
2answers
209 views

How to declare a function that uses local variables of a module outside this module?

I have the following code Module[{f,x=0}, f[t_] := x + t; ..........; ...f[1]...; ..........; ] My aim is to put the declaration of the function ...
3
votes
1answer
213 views

Evaluation of self-defined functions

I defined a function that I call disc which I want to minimize later. The function is defined as ...
3
votes
3answers
163 views

Function that prints a different value than it returns

I want to create a function something like this: test[x_] := (Print[1]; Return[2];) Except it prints 1 when executed as ...
3
votes
2answers
350 views

Differential operator squared

I defined a one-dimensional momentum operator $\hat{p}=-i\hbar\frac{\partial}{\partial{x}}$ in Mathematica p = -I * h * D[#, x]& and I want to get the ...
3
votes
2answers
336 views

Why does the first derivative of a piecewise continuous function have discontinuities?

I have this piecewise continuous function which is also continuously differentiable over time : ...
3
votes
1answer
134 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] ...
3
votes
5answers
283 views

How to add periodicity to condition?

Is it possible to set the following function f[a], where a can be any real number? $$ f(\alpha) = \begin{cases} 1 ...
3
votes
4answers
240 views

Defining a pure function involving a function argument

I've got an expression involving x and y that I want to use as a MeshFunction: ...
3
votes
2answers
165 views

Multiplicative partition function

I am trying to create a multiplicative partition function that would generate something like ...
3
votes
1answer
194 views

Extracting function calls from an expression

I have an expression like this: expr = F[0, 1] + 2 F[0, 0] - 17*F[2, 0] and I want to transform it to a list consisting of all function calls with its arguments: ...
3
votes
2answers
549 views

Ways to plot interpolating functions more economically?

I am interpolating a lot of data over geographic coordinates obtained via GPS, and frequently I need to plot these interpolated functions together. For example, I may have an interpolated elevation ...
3
votes
3answers
137 views

How to identify patterns in a tensor-polynomial and replace appropriate symbol?

I want to write a function tensorReplace[input] that takes a tensor polynomial in $r_i$ such as $r_i r_j r_k+r_i q_j q_k +q_i q_j q_k$ , and replaces each monomial ...
3
votes
2answers
152 views

Apply UpValues before Listability

I'm trying to modify Plus but am running into trouble with it being Listable: ...
3
votes
3answers
88 views

Defining a function that that evaluates a function to obtain a value not supplied as an optional argument

I have a function like this CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_] Now, I want to add a new parameter ...
3
votes
1answer
121 views

Extract data from WeatherData as a list of associations

Given weather data for 2013 and 2014 as follows: ...
3
votes
1answer
138 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 ...
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 ...
3
votes
1answer
259 views

NestList with a list inside?

I'm new to Mathematica, and was looking for more efficient ways to implement the Do loop below. a, ...
3
votes
1answer
131 views

How can I speed up my compiled function that is returned by another function?

Overview I have a function that acts like the Interpolation on sparse $n$-dimensional data using a simple implementation of RBF interpolation method. I want my ...
3
votes
2answers
105 views

How to apply multiple/complicated requirements for a pattern in a function input [duplicate]

In this question. I suggested that to test whether the input of a function was a matrix of numbers one could create the following test: ...
3
votes
3answers
167 views

Function[] argument nesting for operations like Select[]

The following code selects a special element from a list thelist = {"this", "notthis"}; Select[thelist, (# == "this") &] What I want is a function which ...
3
votes
2answers
59 views

Fast functional method for multiplying duplication numbers in a large list

Consider a list of symbols: mySet = {a, b, b, c, a, b, c, d}; I would like to form the product of duplication numbers. In the example above, I have 2 ...
3
votes
2answers
82 views

How to define a nestable Baseform function, BaseForm output becomes BaseForm input

Mathematica won't change a number's BaseForm recursively: 63969 // BaseForm[#, 16] & // BaseForm[#, 8] & // BaseForm[#, 2] & Well, I inspected former ...
3
votes
1answer
203 views

Gateaux (directional) derivatives and higher order differentials of a functional

I would like to calculate the Gateaux derivative of a functional (i.e. a function depending on functions). A simple example for the Dirichlet functional: $L(u(x))=\int_0^1 \frac{1}{2} (u'(x))^2 dx$ ...
3
votes
1answer
117 views

Custom operators; custom pattern matching with pure functions

What I'm trying to achieve in Mathematica is the creation of a binary operator whose operands are both pure functions over the natural numbers. The result of the operator should be another pure ...
3
votes
1answer
109 views

Use of StringExpression as argument

Is it possible to use a StringExpression as a pattern for an argument? I.E. something like foo[chars__~~ WordCharacter.. ~~ num:NumberString] := {chars, num}; At ...
3
votes
1answer
107 views

Is it safe to assign a variable and function of the same name for different things?

I'm writing out a notebook that goes through the van der Waals Equation of State for gases, and I run into a situation where I want to assign (simplified) Tc[b_]:=5b, use that to solve for b in terms ...
3
votes
1answer
239 views

Permuting indices to form fully symmetric tensor with repeated indices

I have written a function (version 8) that takes as an input a list of indices such as {i,j,k} and outputs a fully symmetric tensor function containing $p_i$ and ...
3
votes
1answer
56 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. ...
3
votes
1answer
165 views

Define a function with the condition that it must be positive

I have a function f(x) and I want to define a new function g(x) that is equal to f(x) only ...
3
votes
3answers
695 views

Implementing Newton's method

I have this question on coding Newton's method in Mathematica. I have some code to go by but I have no clue if it's computing the functions in the right order. The book is the numerical methods ...
3
votes
2answers
267 views

Force Mathematica not to reduce a fraction

I am interested in a function which would stop Mathematica reducing fraction. If my input is 50/100, Mathematica outputs 1/2, I ...
3
votes
1answer
138 views

Using NestWhileList to determine smallest prime value in series

I have a function recursively defined as follows: $a_{n+1}-1=(a_n-1)\times lpf(a_n)$, whe $lpf(x)$ is the least prime factor of $x$. Now, given an initial value of $a_0$, I would like to find the ...
3
votes
1answer
147 views

Downvalues vs. Scoping for Functions

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but ...