Questions tagged [function-construction]

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.

170 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
10
votes
0answers
173 views

Does Compilation option “InlineExternalDefinitions”/“InlineCompiledFunctions” lead to unnecessarily complicated nesting?

Question: When an external (possibly complicated) definition f is used inside a compiled function with ...
9
votes
0answers
206 views

What is the best and most efficient way to curry?

Suppose I have a function with a signature like g[expr, _?(f), {1}, Heads -> False] and I want to access it as an operator by giving the first argument last. I ...
9
votes
0answers
113 views

Fast Destructuring

This might be a XY problem. I first post my immediate question and some background below. Question: What's the fastest way to pass arguments matching ...
8
votes
0answers
158 views

KeyValuePattern specificity in DownValues

Bug introduced in 10.4.0 and persisting through 11.3.0 CASE:3824590 [...] Developers are discussing this behavior. [...] I will let you know when I have definite information on this. [...] ...
4
votes
0answers
542 views

Wick Contractions; help with a piece of code

An alternative title to this thread could be, understanding another person's code. A few years back there was a workshop on Mathematica and applications to theoretical physics; if you want I can ...
4
votes
0answers
317 views

Strange behaviour of MMA in derivatives of some standard functions

There are some peculiar things to be discovered in derivatives of some standard functions in MMA: Strange behaviour Example 1: Abs We have ...
3
votes
0answers
71 views

What are some ways to overload Min in order for it to work with user defined expressions?

I have defined size to work with discrete size labels: ...
3
votes
0answers
64 views

Prime Matrix with determinant of powers $2^x$

Mathematica has commands for finding prime matrices, for example, here is a matrix with randoms in the range $<100$: RandomPrime[100, {3, 3}] This $2 \times 2$ ...
3
votes
0answers
48 views

Differences between u[x,y,t] vs. u[x,y][t] vs. u[x][y][t]

Is there any difference between defining a variable by u[x,y,t] or u[x,y][t] or u[x][y][t] ? ...
3
votes
0answers
238 views

How to achieve with Mathematica the effect a closure has in other languages?

Motivation I want to write an auxiliary function (brief auxf) for testing the output of another function that evaluates its body repeatedly using ...
3
votes
0answers
74 views

What's the role of the lines order in defining a function?

I'm totally surprised by the finding I had never noticed before. This code works as obviously intended ...
3
votes
0answers
155 views

How to reduce computation time for a large symbolic matrix

I have a 6x6 matrix (A) that is a function of k and H with complex symbolic entries that contain functions. I want to reduce the computational time since I will be evaluating the matrix many times for ...
3
votes
0answers
378 views

Generating a list of random numbers that follows a pattern

I'm a beginner to Mathematica. I'm writing a code to design a 4-bar linkage mechanism that traces a path in 2D space. I got the modelling done right, now I'm writing my own implementation of a genetic ...
3
votes
0answers
117 views

How to get this terrible summation/product to run in Mathematica?

I've come across this formula and have no idea where to even start. (My assumption here is that $m,n$ are known and input into the expression to arrive at an answer.) $$f(m,n) = \sum_{\substack{0 \...
3
votes
0answers
323 views

Lyapunov Exponent as a function of parameter

I am working at Rossler system and I would like to ask you for some help. This system is described by equations: \begin{array}{ll} \dot{x} = -y - z \\ \dot{y} = x + 0.2y \\ \label{eq:UkladRosSP} \dot{...
3
votes
0answers
130 views

How to make a new analytic function work seamlessly with Mathematica's analysis functions?

I am defining a new analytic function of six variables. I have algorithms to evaluate it numerically, and have amassed a large catalog of analytic properties. It is easy for me to program the ...
2
votes
1answer
43 views

Why some variables get substituted and others don't

I was running into some trouble with my math and I whittled it down to this nugget: ...
2
votes
0answers
34 views

How to define tensors similar to Riemann tensor in xAct xTensor xTras

I'm working on a project where we deal with objects very similar to Riemann tensor. In fact they are Christoffel symbols for higher spins. I'm using xTras for modeling problems in wolfram mathematica. ...
2
votes
0answers
54 views

How to override a built-in Mathematica function using its old version?

How can we override a Mathematica built-in function using itself in the definition? For example, the DiracDelta function is not defined for complex numbers like: <...
2
votes
0answers
44 views

How to handle Units when Integrating?

I have some functions defined, which are to use units. Now another function is used to integrate over those, with the integration variable also quantified and the input to said function. I better just ...
2
votes
0answers
49 views

Speeding up the process of NDSolve[] when a user-defined function is involved?

I am trying to tackle a (1+4 dimensional PDE) model at which the solution of the first PDE (with some interpolations and changing the domain) would be used in the second PDE. In fact, I must choose ...
2
votes
0answers
94 views

Lambda calculus reduction in Mathematica

Are there any built-in functions for reducing lambda terms in Mathematica? If not, how might one do it? I've heard of Function, which is good for $\beta$-reduction, ...
2
votes
0answers
54 views

Fast way to check if an expression matches any of the rules in a list?

I have a function defined roughly like so. This is a minimum recreation. foo[expr_, rules: {__Rule}] := "some fancy result" /; AnyRuleMatchesQ[expr, rules]; What ...
2
votes
0answers
98 views

Why does the evaluation of “Catch[Break[],\$Failed]” not return “\$Failed”?

Under normal conditions, evaluating something that emits a Message eg the evaluation of 1/0 that produces a ...
2
votes
0answers
34 views

Keyed eigensystem for nested Association

Suppose a matrix is given where both rows and columns are keyed, for example this correlation matrix: ...
2
votes
0answers
33 views

Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
2
votes
0answers
100 views

Defining a function by coordinates of points and degree of a polynomial

I'm studying about solid of revolution. In these studies, I learned that to get the volume of such a solid, I need to know $f(x)$ to describe the contour of the profile. This concept I understood. $...
2
votes
0answers
69 views

3 parameter definition for solid modeling

...
2
votes
0answers
75 views

Obtaining the interpolation function of a surface

Some sample data ...
2
votes
0answers
69 views

Built-in functions for Homotopy group

Does Mathematica have built-in functions for Homotopy group, for example for computing $$\pi_n(S^m)=?$$ or more generally $n$-th homotopy group, $$\pi_n(X)=?$$ for some $X$. (I search, but I could ...
2
votes
0answers
71 views

Is there an efficient way to monitor the progress of a task remotely?

Recently, I've been running Mathematica packages on a remote cluster using SSH (using, e.g. the command ...
2
votes
0answers
123 views

Obtaining a Previously Constructed Function as Desired Output

I was playing around with some Generalized Trigonometric Functions defined as follows; ...
2
votes
0answers
70 views

First position of incremental series

I am still a beginner and maybe I want to do something too complicate! After 29 cycles I obtained the following events list: ...
2
votes
0answers
56 views

Rescale large numerical factors in rational functions

Given a rational function $$ f(x_1,x_2) = \dfrac{r_1 x_1^2 + r_2 x_2}{r_3 x_1 + r_4 x_2}, $$ with $r_i$ arbitrary real or complex numbers, is there a built-in function to get Mathemtica to rewrite as $...
2
votes
0answers
311 views

How to Implement a “Function Object” suitable for NDSolve and Finite Element Method?

I have a bunch of methods that shoud return an expression that behave like a function, i.e. an expression that, when supplied with suitable arguments, evaluate to something. A simple ...
2
votes
0answers
152 views

Find algorithm or generator formula with automated procedure and pattern recognition

I am trying to find a generator formula for the discriminant of a polynomial and believe it would be an interesting problem for the Mathematica community to know how to automate evaluation of certain ...
2
votes
0answers
137 views

How to create a complex variable from dated list values

Can anyone help me to construct a variable "PI" from values of variable x. Variable x represents a group of two time series rj and ri on equal dates(joined two dated lists with states).Variable x have ...
2
votes
0answers
457 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
149 views

correct usage of ParallelTable

I am trying to parallelize my script. Here is a MWE of how I have done it: ...
2
votes
0answers
638 views

Generating a function which outputs possible chemical reactions

I want to make a list of chemical reactions and I write them down in a $\require{mhchem}\LaTeX$ format. They are of the following form $$NA_n^i+MB_m^j \rightarrow \hat NA_{\hat n}^{\hat i}+\hat MA_{\...
1
vote
0answers
43 views

Passing python data types as function arguments

I've a few python data types ...
1
vote
0answers
77 views

Approximation of a function by a polynomial (Chebyshev First Kind, Bernstein, etc…) containing only even degrees and constants in a given Range[a,b]

In Mathematica, how can I create a polynomial function containing only even degrees and constants? That is, I have a function: $f(x)=\frac{\pi ^2}{\left(\frac{\pi }{2}-\tan ^{-1}(k (x-1))\right)^2}$ ...
1
vote
0answers
45 views

Using `Block` with Conditions `/;`

I would like Block to run conditionally. I understand how to do one condition, but what about an AND operation? e.g ...
1
vote
0answers
34 views

How to return a variable name and value from within a self-contained function block?

Abstract: I have viewed other solutions on StackExchange but none actually address using a fully self-contained function to return name and value of a variable. The only function that works requires ...
1
vote
0answers
27 views

How to avoid delayed evaluation in Condition used in a function definition

I would like to define a function (below, f) that evaluates only under the condition that some expression is True. The condition is to be specified now, and I don't want it to change for the purposes ...
1
vote
0answers
33 views

Unexpected output of a Listable function. Arguments not threaded correctly

I saw this question asking about the Listable. The accepted answer did a good explanation on the poor performance of user defined function with Listable. However the answer didn't clarify why the <...
1
vote
0answers
31 views

Is there a way to get the calling information inside the body of a function defined using down value?

Suppose a function has multiple argument structure like follows: ...
1
vote
0answers
42 views

Efficient indexing when index must cycle

I've defined a function indexed through a cycle: ...
1
vote
0answers
79 views

Issue with Solve, NSolve, FindRoot

I have this code that I've been trying to get working for some time. I asked some questions before which helped but still struggling! ...
1
vote
0answers
35 views

Plot is passing a bizarrely shifted x_min value to the user-specified Ticks function

The documentation for Mathematica's Ticks option says that you can specify the ticks for an axis by passing a function rather than a plain list description. The ...