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
0
votes
1answer
32 views

Why aren't my variables evaluated in this function?

Consider this code : ...
0
votes
0answers
38 views

Function that once called “erase” every other definition of my notebook

I encounter a really weird effect in Mathematica and I don't know what the wrong thing is nor how to correct it. I work with vectors of four components and I have a minimal set of definitions that I ...
3
votes
2answers
231 views

Outputting expressions in the order in which they're entered

Background For display purposes, I sometimes find it desirable to defeat Mathematica's canonical ordering of variables, and instead have it output terms in the order in which I type them. E.g., ...
2
votes
0answers
48 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 ...
8
votes
1answer
146 views

Element–wise summation for custom function

I use a custom function/head Operator[low, high] which encodes a generic polynomial expression split into "low" and "high" summands following considerations which are irrelevant to this question. Two ...
0
votes
2answers
55 views

Sum over integer partition with variable function argument

Define $$\hat{X}(Y) = [X,Y] $$ I have known matrices $S_i$ and $V$. I am trying to use Mathematica to define a function which calculates $$ \sum_{\substack{n_1, \ldots, n_k>1\\ n_1+\ldots n_k = ...
9
votes
1answer
202 views

Force evaluation of user-defined function

Consider a function that sorts its arguments based on a function called order ...
1
vote
2answers
67 views

Defining a function that treats input as two unordered lists

I have a function f2 I'd like to code into Mathematica with the property that it takes in two lists, treats those lists as unordered, and maps to a symbolic value. If I were instead using a function ...
0
votes
1answer
59 views

Defining a function with a point of finite discontinuity [closed]

I wrote the code for the function $T$ defined on $[0,1]$ by $T(x)=\frac{x+2}{3}$ if $x\neq\frac{1}{2}$ and $T(\frac{1}{2})=1$ as follows. ...
1
vote
3answers
76 views

How to write a discrete function which depends on pairity

Consider the following function for example $$ k = \sum_{n=0}^{\infty} \Gamma(\frac{n+1}{2}) y(n)$$ $$y(n)=\cases{2^{\frac{n}{2}} \text{ even}\\ 2^{\frac{n+1}{2}} \text{ odd}}$$ How can such function ...
3
votes
3answers
216 views

Work of a force field [closed]

I have a parameter curve in 3d: r1[t_] := {Cos[2 Pi t], Sin[2 Pi t], t} r2[t_] := {1, 0, t} r3[t_] := {1 - 1/2 Sin[Pi t], 0, 1/2 - Cos[Pi t]} with a vector field:...
5
votes
1answer
145 views

Defining multiple functions

I have a matrix with 4 elements that I want to turn into functions without explicitely defininig all the functions "by hand": ...
1
vote
2answers
83 views

Define a partial derivative of a composite function

I would like to define the function $ \dfrac{\partial}{\partial y}\left(\dfrac{f(x,y)}{g(x,y)}\right)$, for example ...
6
votes
5answers
399 views

What is the the proper way to define a function from an expression?

Say after some long computation we get an expression expr=x^2 We do not know what the value of expr beforehand. Now we want ...
2
votes
1answer
58 views

How to define a function inside a module through Evaluate [duplicate]

I am trying to define a function inside a module as following ...
0
votes
1answer
34 views

Defining functions trouble [closed]

I'm a neophyte in Mathematica so this is a very basic question. I'm having trouble with defining the following functions: ...
1
vote
0answers
37 views

what the meaning of function definition in this form? [duplicate]

In the post Mathematica code for Bifurcation Diagram, I noticed that Mark McClure define a function like this: f[a_][x_] := a*x - 1.19 x^3; I wonder its ...
0
votes
2answers
92 views

How to compute the density of a Inverse Wishart Distribution?

How does one compute the density of the inverse of a Wishart distribution in mathematica? The MultivariateStatistics package only has the Wishart density, and not the inverse Wishart... Any help ...
3
votes
2answers
105 views

Truncate a set of lists until a subset of entries matches?

Consider lists of vectors of elements like ...
0
votes
0answers
24 views

Trouble restricting function images

I am following this book https://www.amazon.com/exec/obidos/ASIN/1558606378/ref=nosim/ericstreasuretro with mathematica and I'm having some trouble on my way: 1) Consider the function ...
0
votes
1answer
34 views

Is it possible to define a function like this f[K[1]_,K[2]_,K[3]_] [duplicate]

f[K[1] _, K[2] _, K[3] _] := 32 b^9 + 4 b^3 Cosh[b/2] K[1] K[2] - 4 b^3 Cosh[b/2] K[1] K[3] f[10,10,10] I have an equation which is a function of K[1], K[2] and ...
1
vote
0answers
52 views

Series doesn't seem to work on functions

When you try to use the Multivariable option of Series, it doesn't work as intended, since it keeps terms of higher order than intended. Example: Expand a two-variable gaussian exponential to second ...
0
votes
0answers
39 views

Defining a function that transforms an expression with rules

Question I try to define a function that apply a set of rules to an expression in Mathematica 11.3. However, the function returns a result that is not the same as directly applying the rule to the ...
0
votes
0answers
38 views

Define a function with singular Taylor expansion

With an undefined symbol f command Series[f[x], {x, 0, 1}] returns f[0]+f'[0] x+O[x]^2 ...
0
votes
1answer
28 views

Cut graph at a given edge and return remaining subgraphs?

I would like to have a function that removes an edge from a graph and returns all remaining graphs after the operation. For example for a graph like ...
0
votes
0answers
40 views

Removing numerical instability in plots of functions over large domains

suppose you have a large domain you wish to plot a function over, which depends on numerical values usually around or above you machine precision e.g. ...
2
votes
2answers
160 views

How do I define and plot a function involving random variable? [closed]

I am struggling to define and and plot the following function: $\qquad \sin(x) + 0.15\,u$ where u is a uniform random variable in the range [-1, 1]. How can I define such a function and then plot ...
0
votes
2answers
96 views

Finding the best representation of a numerically-inverted function via InterpolatingPolynomial and/or variations

Below is the routine I am using to sort of represent the numerically-inverted function TP. Basically I am finding a necessary interpolating polynomial ...
6
votes
4answers
350 views

Iteratively strip off simply connected edges in graph?

Consider a set of edges composing a directed graph. For example: ...
0
votes
1answer
64 views

Is there a faster way to compute and resuse this pdf?

I'm trying to compute the pdf of a multivariate normal in mathematica. ...
6
votes
2answers
149 views

Finding $x$, the exponent of a $2^x$ when we need an specific output

Say we need a program Findx[n_Integer, m_Integer] where n is an integer from 1 to 9 and m is an integer from 1 to 1000. The output of the program is a number x, which is the exponent of 2 that ...
6
votes
2answers
140 views

Select connected subsets of integer pairs?

Given a list of integer pairs such as list = { {1,2},{1,3},{2,5},{1,5},{6,7},{6,9},{4,6} }; What is a quick way to separate the pairs into groups in which every ...
1
vote
1answer
123 views

Table of function derivative evaluation

I want to create a table with numerical values of a function derivative by using the following code: ...
0
votes
0answers
42 views

Proper implementation of numerically integrated function inside a differential equation

I have a numerically-inverted function TP which is then fed to the differential equation below. My question is: What is the right way to implement this ...
1
vote
2answers
55 views

Switch applying tests to forms

Is it correct that the Select equivalent of Cases is missing for Switch? I mean a function <...
13
votes
4answers
427 views

Renaming options of custom functions while preserving backwards compatibility

I have a package with many functions. There is an option name that several functions share. I feel that the current name of this option was not the best choice. I would like to rename it, but at the ...
0
votes
1answer
73 views

ListPlot with gradient

I have a dataset in which each element is a list of 3 numbers eg. data={{0,0,0},{1,2,0.7},{3,4,2.3},{3,4,3.3}} I'd like to make a 2 dimensional plot in this way: ...
7
votes
2answers
439 views

Can we make a faster Boole implementation?

I just learned about Mr. Wizard's very efficient code here. ...
2
votes
1answer
50 views

How to define a function that can transform integrands to the unit hypercube?

We know most integrands can easily be transformed to the unit hypercube: $$\int_{a_1}^{b_1}\cdots\int_{a_d}^{b_d}{\rm d}^{d}x\, f(\vec{x})=\int_0^1{\rm d}^dx\,f(\vec{x})\prod_{i=1}^{d}(b_i-a_i)\ ,$$ ...
2
votes
3answers
92 views

Globally Defining a Variable that Depends on Arguments of Functions [duplicate]

I have many delayed-defined functions of many variables, and all the functions share a term containing some of these variables. Thus, I would like to just call the term ...
5
votes
1answer
181 views

Pure function as a variable of another function

In a notebook, I have a function that basically needs to do something like calling a pure function within a function. I would say that what best describes would be an example like: ...
5
votes
5answers
356 views

Appending _?NumericQ automatically to every variable of a function in the definition [duplicate]

I have a list of expressions as follows: as=Table[a[k],{k,1,500}] I would like to append _?NumericQ to every element of this ...
6
votes
3answers
148 views

Substitution rule for sums of similar objects?

Consider objects like ...
5
votes
1answer
168 views

Function[{u}, g[u]] vs. Function[u, g[u]] : In which the behavior between differs?

Function[{u}, g[u]] Function[u, g[u]] The documentation lists both variants without specifying the difference. Does the behavior differ in some circumstances?
1
vote
1answer
80 views

Combine single pulse with Boolean test, and integrate series

I have edited this to include integration: This is a more general case of this question. I want to create a function that gives the integral of a series of single pulses of the form ...
0
votes
0answers
29 views

Define a function using pattern matching and repeated replacement to sum the elements of a list.(exercise in Paul Wellin's Book)

I am reading Paul Wellin "Essentials of Programming in Mathematica". I have a question about his answer to 4.1 Exercise 5. 4.1 Exercise 5: Define a function using pattern matching and repeated ...
0
votes
0answers
54 views

Representing an interpolating function

I just want to figure out how to "represent" or to inject an interpolating function into a numerical calculation. So, suppose i have an interpolating function $int$ and then i want to use NDSolve or ...
3
votes
2answers
81 views

Converting a free variable in a pure function into a parameter that can be varied under iteration

Here I am going, as a matter of illustration, make a large string list: l = Characters[WordList[]]; l = Table[DeleteDuplicates[l[[i]]], {i, Length[l]}]; This ...
0
votes
1answer
76 views

Equation of motion of a system

I am studying the problem in Vibration effects. I would like to obtain the simplified equation of motion in (2.7). ...
2
votes
1answer
76 views

Creating a bound method (partial function application)

I want to bind arguments to methods (e.g. for use in OOP creating a bound method--what Java just calls a method). I realized this is basically just a partial function application. Now I want to make ...