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.

Filter by
Sorted by
Tagged with
3
votes
2answers
57 views

Construct a function that maps strings to functions

The main question is the last 5 lines (including codes) of this article. A[x_, y_] := Evaluate[ToExpression["x^2+y^2"]] A[3,4] Then the output becomes ...
0
votes
2answers
37 views

Using MinValue with NDSolve

A simplified example looks like this. The basic idea is to maximize fHelper by adjusting \[Alpha] for each ...
2
votes
2answers
51 views

Hiding variable names in function definitions

I have a problem where Table and similar functions' substitution variable name can conflict with names in my function it self. Here's a minimal example ...
0
votes
0answers
26 views

Defining a function from an evaluated function stored in a variable

I have the evaluation of a function b, stored in appo. Now I want to define a new function f ...
0
votes
1answer
40 views

Sequence of functions iteratively defined

I would like to iteratively define a vector of functions (called copies in the following) depending on a parameter s and a ...
2
votes
1answer
40 views

Narrowing the range of an interpolating function while keeping integral the same?

Consider an interpolating function derived from some data, e.g. ...
0
votes
1answer
46 views

Creation of a Function

I am trying to create two functions but I am having trouble. The first consists of converting degrees Fahrenheit to Kelvin using this formula (F - 32)*(5/9) + 273 ...
0
votes
0answers
58 views

Implicit function definition and variable change

in my code I solve a non-linear system of ODE to obtain the positive functions RadEnDens and PhiEnDens, everything as a function ...
1
vote
2answers
72 views

fitting a monotonic function?

Let's say I have a few points and derivatives given for a function and I'd like to find the best fit through the data, which is monotonic. For example, if I take a polynomial ansatz, I get ...
2
votes
0answers
68 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, ...
-1
votes
0answers
44 views

Better way to score it [closed]

I have a problem to solve: I have a list of regions such as: Brazil [Data collected since 1900] USA [Data collected since 2000] Australia [Data collected since 2010] Russia [Data collected since 2019]...
4
votes
1answer
48 views

Inverse of vector-valued function

Having a differentiable function $f : \mathbb{R}^2 \longrightarrow \mathbb{R}^2$ such that $\det D(f)$ (Jacobi determinant) does not vanish. How can we get the inverse function $f^{-1} : \mathbb{R}^2 \...
1
vote
1answer
39 views

Having trouble when using a piecewise function

I am trying to define a function where H1[x_]= a previously derived value if 0 < x <100 and H1[x_]=x if x>100 so I have used the following piecewise function: H1[x_]:=Piecewise[{{Evaluate[y[x] /....
1
vote
3answers
91 views

Creating recursive functions using `SetDelayed` and `MapThread`

EDIT: I have slightly modified the question to include recursive function. I want to create some recursive functions using SetDelayed within ...
1
vote
1answer
30 views

Cut a list into two sublists containing alternating chunks? [duplicate]

Consider a list such as list=Range[20] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 } I would like to have a function (e.g. ...
5
votes
3answers
67 views

Approximate strictly positive solution to a linear set of equations?

Consider a positive matrix M and a positive vector b, e.g. ...
4
votes
2answers
76 views

Define a curried anonymous function

I want to define a function like f[x_][y_][z_] := x + 2y + 3z but anonymously. The ways to do this that I've discovered so far are (1) define a normal function ...
15
votes
2answers
285 views

Improving Performance of an XY Monte Carlo

I normally write my Monte Carlo codes in Fortran for speed, but I was doing some quick and dirty work and wrote one in Mathematica for the XY model on a square lattice (see Kosterlitz-Thouless ...
1
vote
1answer
38 views

Mapping function into elements of Sum

I'm trying to set an attribute that's valid for a generic sum, where n and f are arbitrary. de[A_ + B_] := de[A] + de[B] de@Sum[f[i], {i, n}] Is this possible? ...
2
votes
1answer
55 views

Operate on functions with arbitrarily many arguments

Is it possible to define a function that can operate on another function with arbitrarily many arguments? For example, I want to do error propagation using partial derivatives. It is not clear to me ...
6
votes
2answers
140 views

Collect data points in a list into weekly groups?

Consider data of a format such as ...
1
vote
2answers
44 views

Restrictions as string to expression in region plots

I'm trying to build a function that generate a string of restrictons to pass on to a region plot with ToExpression, but I think it fails to pass the plot variables. ...
0
votes
1answer
84 views

Automating a large operation by making a function

I would like to automate the following operation. To be transparent I shall put my code into separate chunks and explain the process. Suppose I have the following: ...
3
votes
2answers
123 views

Automatically bound histogram regions?

Given a histogram of some data, such as e.g. in the following sample https://www.codepile.net/pile/NLlNgP60 ...
3
votes
1answer
51 views

Implementing Simpson's rule with two variables

For one of my lab project I need to integrate a given function (in this example, f1, using Simpson's ruls. When only one variable ...
4
votes
1answer
33 views

Computing Higher Order Tensor of Variable Rank

The following code takes a vector x of variable length, computes the outer product of the vector with itself to form the matrix $\rho$ of dimension $2^n \times 2^n$....
0
votes
1answer
34 views

Functions with Variable Numbers of Arguments: Outer Vector Product

I am trying to write a function that takes a column vector of variable dimension as an input and returns the outer product matrix as an output. What I have tried: ...
1
vote
1answer
40 views

Higher Order Tensor of Variable Rank

I would like to write a function, that matricizes a higher order tensor according to the following rule: Let $\mathcal{A} \in \mathbb{C}^{I_{1} \times I_{2} \times \ldots \times I_{N}}$ be a tensor ...
0
votes
0answers
44 views

Function works outside For loop but not inside, and loop fails spectacularly

I'm building two lists, each to carry a different representation of the same set of objects. One list, list2, is constructed first; then the second list, ...
0
votes
1answer
40 views

WriteLine[] not working once put into a function

I wrote a function that takes two strings, an integer, and an OutputStream, it's supposed to write to. The function is: ...
1
vote
0answers
34 views

How to convert output from Reduce into a piecewise function?

Reduce often provides a much fuller solution than Solve. But it's always in the form of a true statement rather than functions ...
1
vote
0answers
26 views

How to measure the Euclidean distance between each evaluation point and the closest reference point in a 2-D problem? [closed]

I would like to measure the euclidean distance between each evaluation point and the closest reference point in a 2-D problem. Attached my reference and evaluation points to have a look at the inputs....
2
votes
1answer
87 views

How to compile or speed up a function with eigensystem?

I have defined two functions as following: ...
3
votes
2answers
86 views

Function with string explanation for its arguments with equal signs [duplicate]

I want to write a function using string as explanation for its arguments so that the function becomes intuitive for new users. For example, I want to write a function to compute BMI (just for ...
6
votes
1answer
59 views

How to obtain the function of a plotted Bezier curve?

I have some control points and I could just plot the Bezier curve, but I also need to have the function! How can I get the exact function of the plotted Bezier curve?
1
vote
2answers
72 views

Understanding how to loop over different variables in Mathematica

I'm using Mathematica to generate some data, and I'm having some difficulty understanding how my intuition from "regular programming" should transfer over. The code that I'm writing does the following:...
0
votes
1answer
34 views

Defining a function that modifies a list [closed]

I have the a list A and I want to define a function f that takes out the $i$-th element from the list ...
4
votes
3answers
719 views

Function that detects repetitions

I have a large list of 2-tuples and I need a function that lists first values that have at least one repeated second value. Small example: ...
0
votes
2answers
126 views

Specifying that function argument is positive

Struggling with something basic. Suppose when defining f[x_] the outcome depends on Sign[x]. When calling this function, how do I tell Mathematica the sign of the argument? My attempt: ...
0
votes
2answers
39 views

Test if function of multiple variables is positive or negative given variable constraints

I have a function of multiple variables (5 to be exact). I would like to know if this function is positive or negative given certain constraints on each of the 5 variables. Here is a simplified ...
3
votes
1answer
65 views

elegant definition of piecewise linear functions (spline)

As far as I understand, the Spline object is the result of some approximation or interpolation, it is not for directly defining functions. That's a pity, as that ...
8
votes
1answer
2k views

What is -(-2,3,4)?

I was trying to define a function f such that, when its second argument is negative, we have $$ f(a,b,c,d,\dots,n)=f(a,-b,-c,-d,\dots,-n) $$ i.e., we reverse the ...
4
votes
1answer
93 views

Wrong Output in self defined Quaternionic Multiplication

I tried to define quaternionic multiplication in terms of pairs of complex numbers where I identify $\mathbb{C}^2$ with $\mathbb{H}$ via $(z,w)\mapsto z+jw$. Consequently, I used the multiplication ...
0
votes
0answers
60 views

How to list all the possible functions of given structure?

I can run this command : Tuples[{ Tuples[{x, y, z}, 2], {a, b, c}}] to list all combinations that looks like: ...
0
votes
0answers
48 views

Iterative loop with many steps

I want to automate the following code, let me begin by explaining the code first. This code, for representation, is divided into 3 parts. The first is: ...
3
votes
3answers
340 views

How do pure functions differ from functions defined with SetDelayed?

Why is it that f[{{a_, b_}, {c_, d_}}] := (a z + b)/(c z + d) will return (2 + z)/(4 + 3 z) on evaluating ...
1
vote
1answer
78 views

Defining functions from strings

Blank symbol (_) is moved when applying ToExpression to the following string: ...
3
votes
1answer
78 views

Generating all connected graphs from a set of n-valent vertices?

I would like to have a function generateConnected[list_] that, given a set of vertices with pre-defined valence (number of outgoing edges) generates all possible ...
0
votes
1answer
118 views

Table and FindRoot of a two-variable interpolated function

I am having difficulty solving for the roots of the following interpolating function: ...
2
votes
2answers
82 views

How to make a contextual variable?

I have a question that would be trivial in OOP, but I am a newbie to programming in Mathematica, so I'm wondering how to deal with it. Basically, I want to create a package which basically involves a ...