Tagged Questions

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

2
votes
2answers
301 views

Recursive formula involving a piecewise function

I am trying to construct a recursion, g[t], where g[t] = a[t] if g[t-1] = 1 ...
0
votes
2answers
91 views

Elegant way to transform formula with sum into code

I have this formula, and many other which contains sum, I am perfectly able to do the computation over a list, however to do this I need to do it in several manual steps. Therefore, I wonder if there ...
-2
votes
1answer
87 views

Calculations in sequence

I have a this calculation, as you can see it is doing a calculation on each list, from list 1 to list 4 of the datafile. My problem is that using Mapthread to do this calculation does not work because ...
8
votes
1answer
270 views

How can I define a Listable function only apply to a vector?

Consider the function f which should behave as f[{{1, 2}, {3, 4}, {5, 6, 7}}] ...
1
vote
2answers
153 views

Using mathematical notation

Is it possible to write this function in mathematica exactly as shown, and then run the expression on a dataset? Please note I know that there are several other ways to do this. I am only ...
0
votes
1answer
104 views

Logic behind Flatten and Append to in Module

I have a list(kicks), and a function(fix). I am aware of the problem using Loop and Append or AppendTo. My question is why does the Flatten produce the desired output. List: ...
3
votes
2answers
237 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 ...
5
votes
2answers
770 views

How to find the non-differentiable point(s) of a given continuous function?

For example, the non-differentiable point of the function $f(x)=|x|$ is at $x=0$. How to find the non-differentiable points of a continuous function that is defined numerically?
2
votes
1answer
105 views

How to define a function of z[bar], etc

Using Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? I have often ended up wanting to define functions of annotated variables to better match my ...
3
votes
2answers
196 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 ...
1
vote
2answers
455 views
3
votes
1answer
98 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 ...
7
votes
4answers
607 views

Turn expression into function

I'm new to Mathematica and I spent the last hour trying to make the expression ...
6
votes
1answer
134 views

Creating a Block from a list of rules

How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments? The function needs to be ...
1
vote
0answers
138 views

Issue with Coefficient command

I'm trying to used the Coefficient command to extract the numerical values in front of a Chebyshev polynomial. I know that there is a numerical way to do this, presented in numerical recipes, which I ...
12
votes
2answers
176 views

How can one write a robust ListableQ function?

I would like to have a test that determines if a particular function is Listable. In the case of Symbols this is merely a matter of checking Attributes. ...
1
vote
2answers
645 views

Run entire notebook

I have created a notebook, with functions. The only thing that I want to change in the notebook later is the input file. I therefor want to have a button that runs every input step-by-step so that I ...
3
votes
1answer
250 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, ...
16
votes
12answers
1k views

Pick elements of largest absolute value

For example, given list = {{1, -3, -5}, {2, 1, 6}, {0, 2, 4}, {-9, 2, 6}} should return: {-5, 6, 4, -9} Updated I found ...
1
vote
1answer
107 views

User-defined Distance Functions with extra parameters [duplicate]

The Nearest function accepts a customized distance function. For example, a simple case that works well is ...
3
votes
1answer
126 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 ...
7
votes
3answers
187 views

How can I break through Function's HoldAll attribute to force evaluation of a body element?

The question concerns creating a function. Let consider the following code With[{f = Function[t, Cos[t]]}, {First[#], f'[#]} & ] which returns ...
6
votes
3answers
1k views

Simulate MATLAB's meshgrid function

Here is MATLAB's meshgrid. I came up with these implementations in Mathematica: ...
4
votes
4answers
221 views

How to avoid writing $K$ loops?

I want to evaluate a mathematical function of $K$ variables each of variables varying between 1:N. One way to do this is writing $K$ loops and varying variables accordingly, as shown below: ...
2
votes
2answers
229 views

Writing a function that returns a (usable) function

I am trying to create a function that returns Arnold's Cat Map of size n iterated k times. I can get something out of the Function command, but I can't figure out ...
1
vote
0answers
165 views

Positive integrand giving negative answer

I'm integrating a positive function f(t) times sin(t) from 0 to pi/5 and get -38. Actually f is slightly negative for a short time (smallest value ~ -0.0005), but far from enough to explain this. ...
2
votes
0answers
31 views

Detecting a more general pattern [duplicate]

I need a function that, given 2 patterns, determines if the first pattern is more general than the second (i.e. that any expression successfully matched by the second, will be successfully matched by ...
3
votes
1answer
106 views

How to make InverseFunction work on function defined with restriction on arguments

This example works to define InverseFunction on the first argument of a function ...
1
vote
2answers
361 views

Collatz Tool Box — any speed ups possible?

I have created a tool box for experimenting with the Collatz conjecture. Info here: Wolfram MathWorld and here: Wikipedia The patterns I have found allow us to make the conjecture jump through some ...
1
vote
2answers
318 views

How to define a function based on the output of DSolve? [duplicate]

I would like to define a function based on the output of the following DSolve call: ...
7
votes
1answer
148 views

Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
2
votes
1answer
107 views

How can I define a function taking two distinguishable sets of options?

It commonly happens that one wants to define some function foo which in turn calls some other function (e.g., Plot) that can ...
0
votes
0answers
69 views

How to insert a function into OptionsPattern?

I’ve written a function TableauForm to draw a standard Young Tableau. This problem is related to this one, however, the difficulty is that, a function has to be applied to the list in my case. This ...
0
votes
1answer
430 views

Computing the fundamental matrix and its monodromy matrix

Assume $\dot x(t)=v(x,t)$ is a $T$-periodic, with respect to $t$ dynamical system. That is: $x\in\mathbb R^n,\quad t\in\mathbb R,\qquad v(x,t+T)=v(x,t)$. Let $x_0$ be a smooth periodic solution in ...
25
votes
6answers
487 views

Constructing symbol definitions for With

I would like to be able to define two arrays, one containing symbol names and one containing the values of those symbols, for use in constructs such as With. For ...
1
vote
4answers
361 views

Recurrence relation in a table

I am trying to generate a list of x-values for a function using a module, where my x-value must increase by 'a' if the term ...
2
votes
1answer
89 views

Map and Scan ignore HoldFirst

I am trying to write a simple function that takes a list of data and outputs it to a file. I'd like the name of the file to be the same as the variable that holds the list. It seems that giving a ...
1
vote
1answer
209 views

Equating and solving equations that have been generated using table[]

I am in the process of trying to use Mathematica to find the coefficients of transmission and reflection for the wavefunction of a beam of particles tunnelling across any number of identical potential ...
0
votes
2answers
183 views

Unwrapping a list when invoking a function [duplicate]

The minimal working example of my problem is as follows: l = {1, 2, 3, 4} f[a_, b_, c_, d_] = a + b + c + d Now, I'd like to evaluate ...
0
votes
1answer
294 views

Function to marginalize a table over given parameters

Let us say I have generated a table tab with entries {p1,..,pd,chi2[p1,..,pd]} of dimension ...
0
votes
1answer
64 views

Extracting function variables from a list [duplicate]

Evaluating the following func[x_, y_] := x^2 + y^2 listn = {{a, b}, {c, d}, {e, f}}; Map[func[##] &, listn, {1}] yields {func[{a, b}], func[{c, d}], ...
0
votes
1answer
122 views

Unable to import functions from external file

I'm fairly new to Mathematica but I'm in the process of setting up some basic analysis based off someone else's work. They had a number of shared functions stored in a shared code directory and were ...
5
votes
4answers
164 views

Make second argument same as first when only one argument passed to function [duplicate]

Clear[f] f[n_, k_: n] :={n, k} f[x] {x,n} Actually I would like to get: {x,x} The following is one wrong definition, I'm showing that I need the ...
8
votes
1answer
140 views

Is it possible to write a function immune to symbol redefinitions in any Block surrounding its invocation?

Normally, semantics of symbols used in a function definition can be changed by their redefinitions in Blocks surrounding the function invocation: ...
0
votes
0answers
40 views

Nested For Loops that result in no output [duplicate]

I am sure that this is not the most elegant code you will have seen, but I am trying to loop through an expression to see if FultonHarrisn] produces any outputs ...
10
votes
2answers
433 views

How to create custom Graphics primitive?

How to create custom Graphics primitive? It should have the following properties, resembling properties of built-in geometric figures, like ...
8
votes
1answer
121 views

Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?

Consider the following expression: Block[{Print = CopyToClipboard}, doSomething[]] Now all calls to Print from ...
6
votes
1answer
212 views

Pattern definition for replacing plus and subtract

Suppose I have two functions $h_1(x)$ and $h_2(x)$, for which we know their sum and their difference: $$ h_1(x)+h_2(x) = d(x) \qquad h_1(x)-h_2(x) = i f(x) $$ I have a complicated expression, in ...
1
vote
0answers
76 views

How can I compile function with Apply?

For example, I tried to compile this function but Mathematica gives "The only function arguments supported are Times, Plus, or List", I have no a way to compile this. ...