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
2
votes
2answers
72 views

Function as a sum of functions

I just get started with Mathematica and I have a basic question about defining a function (I am sorry if this was asked before). I have 2 functions defined as: ...
0
votes
0answers
88 views
+250

How to use Equal for close arbitrary precision numbers to be considered the same?

I am trying to add a comparator function in a larger piece of code which can equate two arbitrary precision numbers and yields True if they are approximately close. ...
9
votes
2answers
189 views

Looking for a Compilable Form of DeleteDuplicates

Union is Compilable but DeleteDuplicates Isn't?! First, DeleteDuplicates is not compilable ...
5
votes
1answer
245 views

IntegerQ[x] gives false. Just leave it as it is

Since x is an alphabet not an integer, Clear[x];IntegerQ[x] gives False But sometimes I ...
7
votes
1answer
95 views

Compile Can Handle Passing an Empty List Only When Returned by Some Functions?

There seems to be a bug whereby Compile can deal with empty lists only when passed in some cases. Consider the case below. ...
1
vote
1answer
41 views

How to make a vector of elements the pattern of a function?

if I have an expression that contains a lot of symbolic elements and I would want to make it a function of the vector containing all the elements I've found a solution based on rule delayed: ...
1
vote
0answers
84 views

How to make a function ignore an argument [closed]

Motivation I am trying to call a function f, that has either 1 or 2 (or multiple) parameters, from a function g that uses ...
1
vote
1answer
43 views

generate quadratic functions to evaluate membership of points

How can I make a printable list with different quadratic functions and with points that belong or not to them so that a young person can check whether or not the point belongs to the parabola? my code ...
10
votes
1answer
258 views

"instruction 9223372036854775807" Bug and Bug with Using DeleteCases[] inside Compile[]

I get an error referring to "instruction 9223372036854775807" as follows. I run the code ...
1
vote
1answer
57 views

Extract function argument sequence

Is it somehow possible to access and reference the sequence of arguments of a non-pure function? For example if I want to take the derivative with respect to the n'th argument, a pseudocode example ...
0
votes
1answer
51 views

Extracting function from contour plot [closed]

I have made a contour plot via Minitab and got an image like this one: With $z$ being the enthalpy $[BTU/lb]$ $x$ being the temperature $[°F]$ $y$ being the ...
5
votes
2answers
813 views

I believe Beep uses the system sound which is obnoxious and rude. How does one create a friendlier less intrusive sound?

Beep is a terrible sound for alerting when a background task has successfully completed. It was primarily designed for errors or when a task fails. That's why it's ...
2
votes
1answer
65 views

Construct really identical function (the same as Length)

In[1] Length[Unevaluated[1 + 1 + 1 + 1]] Out[1] 4 Q1) Can we construct a function named f, such that ...
1
vote
0answers
59 views

Simplifying a numerical integral involving a variable integration limit

I have a one-variable function basically consisting of an integral whose limits depend on the variable —I mean, something like this: $F(x)=\int_0^{L(x)}{f(x,t)}dt$ with $L(x)=5x$   and   $f(x,t)=t+x^2$...
1
vote
1answer
108 views

My dynamic button evaluates as soon as it's created. How do I stop this behavior?

I think I finally hit on the holy grail of buttons. With a very small footprint I coded a dynamic AutoSave button whose duration and frequency can be set all the while showing very useful dynamic real ...
0
votes
1answer
62 views

Create an interactive list in a Wolfram notebook that choosing its items make a change in calculations

I have 3 variables. I want to create an interactive list of items in a Wolfram notebook that if I choose each item, functions that are written by myself change the value of the variables and ...
3
votes
1answer
65 views

Problem defining a function that takes a binary input to its decimal form

I am trying to define a function which takes a binary number and gives the decimal form. I tried the following binarytodecimal[b_] := NumberForm[2^^b, 20] But the ...
0
votes
1answer
45 views

Define Matrix Function in a For loop

Let $g\in\mathcal R^{3\times 3}$ be a given known rank 2 tensor function, $gInv$ its inverse, and $dg\in\mathcal R^{3\times 3\times 3}$ its gradiant: ...
1
vote
0answers
72 views

Is there a function in Mathematica to combine/decompose sinusoids? [closed]

Lets say I have some combination of an arbitrary number of sinusoids with potentially varying magnitudes, phase angles and frequencies that I would like to compose into a singular sinusoid, is there a ...
7
votes
5answers
380 views

An anonymous function with constraints [duplicate]

I'm not very good at working with anonymous functions, a fact that has now come back to bite me. Suppose I have a function defined as follows: ...
17
votes
4answers
1k views

How to straighten a curve?

What's the easiest way to get the following animation for a given parabola where the arc length is preserved (that is, for two points in the parabola, I straighten it as follows) My main goal is the ...
3
votes
2answers
132 views

What is the usefulness of adding a colon ":" to arguments in the declaration of a function?

I'm studying a code with the following function declaration : FindChain[ x:__ ,y_?FindChainQ,z:__] := { {x},y,{z} } What is the meaning of the colon ":" after the arguments x and z ? What ...
0
votes
2answers
79 views

Writing a wrapper function computing the memory in use

Is there any way I can write a wrapper that wraps around a piece of code and evaluates how much memory has been used after the execution? Ideally I would like a function that performs the following ...
7
votes
0answers
65 views

Why does Longest not work on OptionsPattern?

When having a function with optional arguments and options, I frequently find myself writing something like this: ...
2
votes
2answers
95 views

Find the range of a univariate function over a specific interval of the variable

I would like to find the range of the function below, but only for values of $a$ strictly greater than zero. ...
0
votes
1answer
39 views

Restrict the domain of a function (and plot) it) when it depends on the value of a parameter

I know similar questions have been asked, but I am very new at Mathematica and other questions/answers are quite complex and do not suit the code I want to write. I want to restrict the domain of the ...
0
votes
0answers
42 views

A Function of Arguments or a Function of a List of those Arguments – Which is Recommended?

Which is recommended from a programming perspective and has better performance in memory and speed given guaranteed identical output – a function taking many arguments and then referring to those ...
3
votes
2answers
83 views

Passing Arguments or Passing a List and Referring to its Parts – Which Gives Better Performance?

Which is better for optimizing speed, memory, and coding "safety" – passing arguments to a function or passing a list to the function and call the list's parts? That is, which of the ...
0
votes
1answer
43 views

How to Set Text Format by User-Defined Function with Input as String

I have simple but annoying problem here. I guess it occurs to many Mathematica users. The thing is that I wish to define a function to format the text in Mathematica. Yet, I don't know whether it is ...
3
votes
1answer
110 views

Alternatives in pure function construction

When I declare a function f in the following form: f[a|b] := 1 then I can use it like: ...
0
votes
0answers
33 views

Storing a value in array during a for loop

So I have a piece of code below that is used to make a density plot and make the plot into grids, where I then find the center of each grid and store it in the varible called cp. The code for this is ...
4
votes
1answer
62 views

Apply function modification to curried function

This is based on A function that modifies functions - well-defined operation?, a previous question of mine. Lukas Lang's answer of course works very well, but now I encountered the following edge case:...
1
vote
1answer
44 views

How to pass results of computations to a function definition?

Here is the following code that works, F[{x_,y_,z_}]:={16(y-x), x(45-z)-y, x*y-4z); x = Array[a,3]; F[x] and the answer is, ...
0
votes
1answer
54 views

Putting arguments of function automatically

This is a snippet of a code that I have: ...
1
vote
2answers
131 views

Is there a way to do this more efficiently?

I'm trying to write this Mathematica function to find the optimal price for a model, given some model parameters. This is a stylized example of what I'm trying to do, and I'm wondering what's a better ...
0
votes
0answers
55 views

RegionPlot Calling User-Defined Function Not Working

This is following up on this post: Function not evaluating as expected I am trying to make a RegionPlot with Manipulate in which ...
1
vote
0answers
45 views

Function not evaluating as expected [closed]

I'm not sure I understand why this is not working and why it's giving different answers: A simple example illustrating the issue is below: ...
1
vote
1answer
63 views

How can I pass a list as an index set for product

I want to evaluate a Blaschke product based on the set of zeroes, and compute its preimage for some points. The Blaschke Product looks like: $$B(z) = z^n\prod_{a_k\in \text{zeroes}} \frac{z - a_k}{1 - ...
0
votes
0answers
45 views

How to reconstruct a complicated built-in function (e.g. for modification or use in a version that does not have it yet) [duplicate]

Is it possible to extract the definitions for a built-in function to reconstruct a similar user defined function. This would be useful to port new mathematica functionality to older versions or to ...
8
votes
1answer
452 views

very simple but unexpected aspect of Function[ ]

I've always thought (2#+1)& and Function[x,2x+1] are interchangeable in common sense. But, ...
1
vote
1answer
52 views

Defining a two-variable function with a parity property

I would like to define a function that has the following property $f(-x,y)=f(x,-y)$. An example is $f(x,y)=x y$. For simple inputs, I can impose this easily enough as: ...
2
votes
1answer
80 views

A function that modifies functions - well-defined operation?

Here's my basic setup: Say I have a list of specific symbols and a function noSymbol, which accepts a mathematica expression and checks whether any symbol from my ...
1
vote
2answers
87 views

How to work symbolically with a compiled function?

I have a compiled function f to be used as a piece of a much larger calculation. I would like to expand out a function that calls ...
0
votes
1answer
82 views

how can I modify this function to convert to polar form for any complex expression?

The function from another post is to convert a complex number to polar form. polarForm = Expand[# /. z_?NumericQ :> Abs[z] Exp[I Arg[z]]] & Now if I apply ...
3
votes
2answers
543 views

How to generate all possible functions combinations

Assume that I have 3 functions like this (total 10 functions in reality). f1[x_] := 3 x; f2[x_] := x^2 - 1; f3[x_] := 2 x + 5; Now I want to generate all possible ...
2
votes
1answer
92 views

Why does this DeleteCases not work? [duplicate]

I believe this is simple but I couldn't figure out why this doesn't work. Everything looks good to me. ...
0
votes
1answer
91 views

Defining a function in Mathematica without functional form [closed]

I am trying to define a function $H(s)$ in Mathematica, but the only restrictions/information on this function are that it is a concave and increasing in $s$, which is defined over the interval $[0,S_{...
2
votes
1answer
81 views

How can I make this plot easier to see?

How can I make this plot easier to see? In this plot the waverform is not clear. If I plot with w only from 0 to ...
3
votes
4answers
117 views

Construct Function[ ] from String based formal arguments

If wee look at: x=11 f=Function[{x},x^2] f[5] we get: 11 Function[x, x^2] 25 as expected. Now suppose we want to create the ...
0
votes
2answers
45 views

How to make the third parameter optional and adding some condition?

I have a function as follows. The function has 3 parameters. Now I would like to make the third parameter i optional. So you call call it like ...

1
2 3 4 5
42