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
2 answers
178 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: ...
  • 2,293
0 votes
1 answer
49 views

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

I have the following expression. ...
2 votes
0 answers
77 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 ...
  • 41
2 votes
0 answers
53 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 ...
3 votes
1 answer
195 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. ...
  • 3,529
0 votes
1 answer
79 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$. ...
  • 1,939
1 vote
1 answer
79 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: <...
  • 225
0 votes
1 answer
34 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: ...
0 votes
1 answer
50 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 ...
  • 4,362
4 votes
2 answers
80 views

Filtering out function pairs with domains lying on one side 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 ...
  • 2,822
2 votes
1 answer
50 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....
  • 45
6 votes
1 answer
127 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 ...
  • 2,822
0 votes
0 answers
36 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 ...
  • 2,923
6 votes
1 answer
112 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 ...
1 vote
0 answers
60 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 ...
  • 2,822
0 votes
0 answers
37 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 ...
3 votes
2 answers
256 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,...
  • 353
3 votes
0 answers
51 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. ...
  • 2,822
2 votes
1 answer
80 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 ...
  • 3,529
5 votes
3 answers
275 views

Distribute arguments over a function in all ordered combinations

I'm looking for a function that can do this ...
  • 630
1 vote
2 answers
77 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 ...
  • 273
0 votes
1 answer
69 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: ...
  • 695
0 votes
0 answers
38 views

Define regular function as difference of two piecewise functions

Consider a piecewise smooth function, ...
  • 1,317
3 votes
3 answers
180 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: <...
  • 43
9 votes
3 answers
570 views

How to plot a point with two colors?

How can I use this type of marker for this? ...
  • 2,822
2 votes
2 answers
105 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 ...
  • 2,293
2 votes
1 answer
118 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 ...
1 vote
2 answers
63 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 ...
  • 301
3 votes
3 answers
392 views

Better way to do such iteration

Consider such an iteration function ...
  • 6,625
3 votes
1 answer
71 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 ...
  • 273
3 votes
3 answers
67 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: ...
  • 9,747
1 vote
1 answer
94 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 ...
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 ...
3 votes
3 answers
379 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 (...
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 ...
  • 2,570
2 votes
1 answer
52 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: ...
0 votes
1 answer
61 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; ...
2 votes
3 answers
80 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 ...
1 vote
1 answer
90 views

Trying to understand weird behavior in function? [closed]

I wrote this function: ...
  • 5,061
2 votes
2 answers
49 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. ...
0 votes
1 answer
39 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}$: ...
  • 321
2 votes
2 answers
73 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 ...
2 votes
2 answers
161 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 ...
1 vote
2 answers
106 views

How to have index of summation equal to all $n$-tuples $\left(b_i\right)_{i\le n}$ with $\sum ib_i = n$ and $b_j$ fixed?

Let $T_n$ denote the set of $n$-tuples $\left(b_1, \ldots, b_n \right)$ of non-negative integers such that $$\sum_{i=1}^{n}ib_i=n.$$ I am trying to simplify the sum (whose indicies depend solely on $n,...
1 vote
1 answer
90 views

How to do a 5x5 system using Cramer's rule? [closed]

I need to know how to do a 5x5 system using Cramer's rule.
1 vote
1 answer
128 views

A square board of size n*n [closed]

I would like to Randomly generate n^2 natural numbers in an interval from 1 to n. Then consider placing each number on the cell with the same number (you can imagine the board numbered naturally, row ...
4 votes
1 answer
124 views

Is there analogy of function MapIndexed (Map) for Nest?

We have Map and its upgraded variant MapIndexed. Is there analogy for Nest? I know there is ...
  • 5,915
0 votes
0 answers
41 views

How to assign a variable in mathematica to just the output of an expression?

So, say I wanted to assign a function J[x_, y_] :={D[system, x], D[system, y]}//Transpose where system is some system of ...
2 votes
1 answer
60 views

Print out how many times to calculate the sum of the squares of its digits until answer is 1 or 4

I am trying to write a recursive function, toscare1[], that takes as parameter a positive natural number. Then calculate the sum of the squares of its digits, thus creating a new number; then, we ...
0 votes
0 answers
41 views

Derive a function and then make a fast version for execution

I would like to derive a (complicated) equation of motion and integrate it efficiently numerically. I can do this "by hand" by copying the resulting function in a new function and then ...

1
2 3 4 5
47