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
1 vote
0 answers
72 views

DeleteCases/ReplaceAll and PatternSequence [closed]

I thought it must be ...
imida k's user avatar
  • 4,047
1 vote
2 answers
58 views

Weird behavior of Which with lists [closed]

To demonstrate my problem, let's create a little function: ...
user16320's user avatar
  • 2,366
4 votes
2 answers
230 views

Transferring arguments to a function

I want to pass a list of input arguments to a function. Something like: ...
thai tran's user avatar
2 votes
2 answers
158 views

Returning the value of the last iterators used in a double for loop

This is a basic question - I'm just unfamiliar with Mathematica. For a given $n$, I'd like to find integral $b$ and $k$ such that $bn + k^2$ is a perfect square. I know that $k$ and $b$ are less than $...
NegativeEpsilon's user avatar
1 vote
1 answer
48 views

Understanding Distribute when applied with a user defined function

I would expect this F[x_] := Module[{aux = x}, aux = X]; {a + b + c, e+ f} // Map[Distribute@*F] to yield {3X, 2X} instead, the function is not distributed ...
Albercoc's user avatar
  • 966
1 vote
1 answer
62 views

Function f[a__][b__] with double inputs and definitions/automatic rules

I have a function of the form f[a___][b___] which I would like to apply rules to, for example: ...
Akoben's user avatar
  • 727
1 vote
2 answers
49 views

Multilinear Function

I have a totally antisymmetric function of n arguments, f[a,b,c,d] = -f[b,a,c,d], which I would like to be multi-linear, e.g. ...
Akoben's user avatar
  • 727
2 votes
2 answers
201 views

Sorting a List Based on a Given Order

Suppose I have the following input: input={{1, 2, 3}, {-2, 4, 6}, {-1, 3, 6}} How can I sort it according to this list: ...
emnha's user avatar
  • 2,411
0 votes
1 answer
59 views

Equivalent Uses of SetDelayed Result in Different Results When Defining an Existing Variable

I have the following expression. ...
Vasil Avramov's user avatar
2 votes
0 answers
114 views

How to write 2D finite volume solver in a functional approach?

I'm solving 2D poisson problem of a finite volume problem using TDMA. The solution of the problem is given: \[Phi][x_, y_] := x^2 y + x y^2 + 1 The definitions are ...
kpaz's user avatar
  • 41
2 votes
0 answers
69 views

Argument value loss using Listable C-Compiled Function

In my recent studies, I have come across a very strange anomaly with C compiled functions. When I call a function that has the 'Listable' attribute while global variables are accessed through ...
Michael Haring's user avatar
3 votes
1 answer
213 views

Test whether an expression contains a particular pattern

How to test whether an expression contains a particular pattern ? I made a function for myself that tests whether an expression contains Plus or not. ...
imida k's user avatar
  • 4,047
0 votes
1 answer
87 views

When running code to compute a sequence, loop variable gets set to a constant

I would like to write Mathematica code to read in a list of sequences, and print the sequence definition ($a_n$), along with the sequence evaluated at a few values of $n$. ...
mjw's user avatar
  • 2,096
1 vote
1 answer
110 views

How to convert a function with named arguments to a pure function?

A function is defined with named arguments. How can this function be converted to a pure function that uses slots (#). As a simple example, how can the following: <...
feynman's user avatar
  • 229
0 votes
1 answer
37 views

Properly defining functions after they have been used in expressions [closed]

I am having trouble with how functions behave when defined, after they have been used in an expression. More specifically, I start with an expression of the form: ...
Vasil Avramov's user avatar
0 votes
1 answer
54 views

How to perturb data along a midline?

Consider the following data, which can be obtained here ListLinePlot[data, Frame -> True] I want to perturb the previous data around a defined midline, which ...
sam wolfe's user avatar
  • 4,179
5 votes
4 answers
178 views

Filtering out function pairs with domains lying on one side (left or right) of an intersection point

I have a list of pairs of functions that intersect at one point, as shown in the image. I am attempting to remove pairs where the domain of both functions in the pair lie on one side of the ...
hana's user avatar
  • 2,688
2 votes
1 answer
54 views

Using the result of Solve as a function of parameters

I am trying to find b2 maximising a function I named 'EG1EUB'. And 'EG1EUBb2' is the frist order condition. Based on my understanding, Solve gave me a single solution saying that b2 is the second root....
H_Kim's user avatar
  • 45
6 votes
1 answer
154 views

How to keep lines on boundary or inside a region?

This post is related to one of my previous posts. My goal is to keep only the lines that fall on the boundary (2, 8) or inside the region ...
hana's user avatar
  • 2,688
0 votes
0 answers
38 views

Syntax highlighting like Function?

I'd like to define a symbol that behaves similar to Function. Specifically, I want to define a new symbol symmetricFunction that ...
JEM_Mosig's user avatar
  • 2,953
6 votes
1 answer
258 views

Sort the nested list by the sublist content; sublists of different lengths

I would like to write the function NewSort function that sorts a list of sorted sublists, basing first on the canonical ordering of subsequent sublist elements. In ...
wedelfach's user avatar
1 vote
0 answers
64 views

How to make the thickness of the line constant?

I created this code based on a previous post, with some modifications to plot a line with a 30-degree slash. It partially works, but the thickness of the slash varies from line to line. How can I make ...
hana's user avatar
  • 2,688
0 votes
0 answers
44 views

Nest n-times a multi-variable function with only one of the variables being updated with previous function value

I have a function y1 of three variables, x, y, and z. (y1 also depends upon f1 defined previously). I would like to Nest or interate, such that on the nth iteration, the y1's 3rd variable, z, will be ...
Thomas 's user avatar
3 votes
2 answers
273 views

How to write a function that performs two operations? [closed]

I am solving an entry-level exercise to practice on writing my own functions in Mathematica. I want to build a function that accepts a matrix of size larger than 2×2. The function will replace the {1,...
tush's user avatar
  • 375
3 votes
0 answers
52 views

How can one automatically arrange the connections of a circuit with as few crossings as possible, given a set of edges?

Here are two functions to create symbols of dc voltage source and a resistor. ...
hana's user avatar
  • 2,688
2 votes
1 answer
83 views

Defining function for decimal value k[0.5+0.5]

k[1 + 1] = 2; k[1 + 0.5] = 1.5; k[0.5 + 0.5] = 1; {k[2], k[1.5], k[1]} // TableForm 2 1.5 k[1] Can you explain this phenomenon? I think the output should be ...
imida k's user avatar
  • 4,047
5 votes
3 answers
286 views

Distribute arguments over a function in all ordered combinations

I'm looking for a function that can do this ...
Albercoc's user avatar
  • 966
2 votes
2 answers
82 views

Would it be slower to place function definitions inside another function definition?

Will it be slower to place functions' defintions inside a main function? I usually do that if the subfunctions are short. However, with a long subfunction I usually place it outside as I think that ...
internet's user avatar
  • 507
0 votes
1 answer
107 views

Tell Mathematica how to //Expand a function

I have a function DP that takes two arguments, that is linear in both, i.e. DP[a,b+c] = DP[a,b] + DP[a,c]. I am using the following: ...
Akoben's user avatar
  • 727
0 votes
0 answers
43 views

Define regular function as difference of two piecewise functions

Consider a piecewise smooth function, ...
Patrick.B's user avatar
  • 1,359
3 votes
3 answers
201 views

Building A Function Using Constants From a List

I want to build a function of the form $f(t)= C + \sum_i A_i*Sin(f_i*t+\phi_i)$ Where $(A_i,f_i,\phi_i)$ are amplitudes, frequencies and phases that I have stored in a text file. Here is my attempt: <...
John's user avatar
  • 85
9 votes
3 answers
593 views

How to plot a point with two colors?

How can I use this type of marker for this? ...
hana's user avatar
  • 2,688
2 votes
2 answers
115 views

How to gather functions which intersect at the same point?

How to gather functions which intersect at the same point (real solution)? func = {2 x, x^2 + 1, (x^3 + 3)/2}; For example, with the list of functions above I ...
emnha's user avatar
  • 2,411
2 votes
1 answer
121 views

Using the Apply construction

I am puzzled about the use of Apply (@@). Consider g = t; g1 = Function @@ {t, g} g2 = Function[t, g] Why does g2 differ from g1? I had thought they would be ...
John Bechhoefer's user avatar
1 vote
2 answers
67 views

Function defined through conditional pattern and derivative

If I define a function through a conditional pattern, its derivative is not defined in the whole domain of the original function... Minimal code to reproduce : for instance, I define ...
coussin's user avatar
  • 301
3 votes
3 answers
406 views

Better way to do such iteration

Consider such an iteration function ...
matrix42's user avatar
  • 6,754
4 votes
1 answer
151 views

How to delete edges from a graph with some condition?

I want to delete an edge and its reversed edge from a graph (if they exist in the graph). It would be OK to use EdgeDelete[g, {edge, Reverse[edge]}] if these two ...
internet's user avatar
  • 507
3 votes
3 answers
76 views

Sort a list using a scoring list with Switch [duplicate]

I have a minimal working example (MWE) list called listToSort that is a list of string triplets: ...
Andrew's user avatar
  • 10.4k
1 vote
1 answer
103 views

Define a function on a MeshRegion

This looks like a simple question with the answer to be easily found, but unfortunately I could not find it either on the web or in the documentation browser. Let us assume we have a MeshRegion ...
Jürgen Böhm's user avatar
5 votes
5 answers
2k views

How can I ignore an argument?

I made some code (attached below). It is meant to make a list of random numbers that is not divisible by some value k. I wish to be able to not enter a value k, and have the code run regardless. It ...
Robin Hansen's user avatar
3 votes
3 answers
442 views

How to make a function take another function as an input?

I am quite new to mathematica, and I simply want to know how to make a custom function, take another arbitrary function as an input. I have attached code that shows what I am trying to accomplish (...
Robin Hansen's user avatar
15 votes
4 answers
2k views

Automating interesting ways to write 2023

Inspired by Interesting ways to write 2023, I was wondering if there are ways to automate the search for results using MMA. I tried ...
Moo's user avatar
  • 3,044
2 votes
1 answer
62 views

Failing to define a function? [duplicate]

I am trying to define a function and it apparently is not working. Here is my small working example: ...
Granger Obliviate's user avatar
0 votes
1 answer
130 views

Create a function that can assign values to different variables in Mathematica

Here I am trying to create a function that can assign values to different variables in Mathematica. Assume the function f[a _, b _, c _]; x1 = a; x2 = b; x3 = c; ...
Math View's user avatar
3 votes
4 answers
123 views

Constructing a function that returns two distinct values by Mathematica

I'm trying to enter a function in Mathematica that can take two inputs, a, and b, and return two outcomes based on those two numbers. I will assume the name of this function as Resultsf[a _,b _] the ...
Math View's user avatar
1 vote
1 answer
96 views

Trying to understand weird behavior in function? [closed]

I wrote this function: ...
Red Banana's user avatar
  • 5,121
2 votes
2 answers
74 views

Is it possible to combine a default value in a function definition with type-checking, in case the optional parameter is actually given in a call?

1.) In a function definition, e.g. ...
Adalbert Hanßen's user avatar
1 vote
1 answer
62 views

Piecewise function with form determined by current value

I would like to create a piecewise function that determines the dynamics of a variable $x_t$ as follows: Given a starting value $x_0$ and a parameter $\hat{x}$: ...
NC520's user avatar
  • 407
2 votes
2 answers
164 views

How does one pass an arbitrary expression (or expressions) to a function to later be evaluated? [closed]

This isn't my exact use case, but I think the answer will still be applicable to what I'm doing. I essentially want to create a function with which I can pass an expression (or list thereof), certain ...
sjtumlin16's user avatar
2 votes
2 answers
168 views

Returning an InterpolatingFunction

I am trying to write a function using Block that generates an InterpolatingFunction and then generates a second InterpolatingFunction that is a function of the first. I can return and use the first ...
John Bechhoefer's user avatar

1 2
3
4 5
49