Questions tagged [programming]

Questions on writing non-trivial programs in Mathematica. Do not use this tag for questions on plotting/graphics or for questions on doing mathematics with Mathematica, where the focus is more on the math than the program.

Filter by
Sorted by
Tagged with
0
votes
0answers
47 views

Label/GoTo and Compound Expressions

My Mathematica notebooks are often many pages long and, for clarity, I generally write only one expression on any line. In operation, such notebooks need to jump around within the full length of their ...
0
votes
1answer
86 views

Classify across ten methods

Mathematica supports ten different methods for Classify. The ten methods are: "ClassDistributions", "DecisionTree", "GradientBoostedTrees", "LogisticRegression&...
0
votes
0answers
31 views

How to save previous calculation

I want to calculate the longest chain up on million applying repetily the Collatz function until it reach one. I was able to solve this using the following code: This is the definition of the ...
-1
votes
1answer
44 views

Dirichlet theorem [closed]

I'm programming Dirichlet theorem - here(p mod d=a, p is a prime number) via procedural programming and I don't have any idea, how to start. I would like to have function, which gets three numbers (a,...
3
votes
0answers
75 views

Module inside With, With inside Module, or Just Module?

In general, if one wants to define local variables and local constants, which of the following is the best programming practice in terms of performance and code "safety"? (Assume ...
13
votes
1answer
227 views

Programming language prototyping in Mathematica

Are you aware of any projects using the Wolfram language resp. Mathematica as an environment to explore the design of programming languages - in particular languages with a focus on mathematics (...
4
votes
1answer
53 views

Best way to write a function that randomly takes definition from list of functions?

I want to define a function based on a random choice from a list of possible functions. For example, suppose the possible functions are {Sqrt,Log}. Then I want to ...
21
votes
6answers
3k views

Creating the Yin Yang symbol with minimal code

The following code creates the Yin Yang symbol ...
0
votes
2answers
38 views

Function providing input and integration limits for NIntegrate

I am trying to define some custom function that evaluates the numeric integral of some complicated function. The problem is that I would like to include as an input the integration limits. A MWE ...
1
vote
0answers
70 views

Can we make “applets” with Mathematica?

I've made a very simple photo resizer with Mathematica: It takes some files in a folder, processes them and write them into that folder. Now, I have to open the Mathematica file every time and ...
0
votes
0answers
31 views

Standard way to define a linear operator?

Say I want to define some simple linear operator. I can do this by f[c_?NumericQ x_] := c f[x] f[x_ + y_] := f[x] + f[y] Or let's define a bi-linear operator ...
0
votes
0answers
29 views

Inequality programming involving sum compositions

$n=3$, $m=3$, $B$ - identity matrix $3 \times 3$ Trying to implement it in Mathematica, but can't figure out how to program the second term. The result is an error. ...
0
votes
0answers
46 views

Hi. I'm a beginner so I need help to solve this equations using Regula-Falsi to find root of the equation (which is x)

I've been trying to look for an example but only with one unknown. In my case of studies, I have 4 unknowns. I just want to make sure is it correct the way I'm writing the coding to find the root ...
0
votes
1answer
91 views

Find the Jacobian and Wronskian of 3x3 system [closed]

Can you help me find the code that calculates the matrix Jacobian and Wronskian of 3x3 type. A matrix with the help of the "modlye" commend. I can only write this: ...
0
votes
0answers
48 views

Force WhenEvent and NDSolve to numerical evaluation and Best Practice

I'm trying to perform an hybrid kalman filter for a nonlinear system. The following code works but it is very slow due to the fact that it does symbolic evaluation. What I wish is that for the filter ...
4
votes
1answer
210 views

Programming twisted pyramids based on polygon spirals

I'm interested in polygonal spirals and after reading Filling Space with Pursuit Polygons, I was curious if I could extend the program to create twisted pyramids as 3D-objects. My goal is something ...
1
vote
2answers
57 views

WhenEvent inside Ndsolve how to evaluate actions numerically every time the event happens

I need to force a whenevent action to be evaluated numerically all the times the event happens. The fact is that I use very complicated matrices and I need to invert them so the system requires a lot ...
2
votes
1answer
43 views

In NMinimize, how to incorporate constraints on positive semidefiniteness of a matrix which is not the matrix variable being optimised?

The problem I want to solve the following problem for symmetric matrix $X$: $$ \begin{aligned} \min_{X\succ 0} \; & -\log(\det(X)) & \\ \text{subject to} \; & \begin{pmatrix} X &...
2
votes
1answer
85 views

RLink fails in Mathematica 12.2.0 Mac OS Catalina

Cannot run RLink in mathematica in Catalina. I get a failed error even though the paclet loads. Any pointers?
5
votes
1answer
101 views

Is there a better way to simulate quadratic cost MPC problems?

Question I would like to know if there is a more straightforward or a better way to simulate MPC problems in terms of code length and accuracy as compared to what I am doing. I am looking to know if ...
0
votes
0answers
47 views

Need help to understand the code

Here is the code by P. Valko and J. Abate for finding numerical inverse Laplace transform by trapezoidal's rule. Can someone willing to explain this code? I don't understand this part: ...
0
votes
2answers
55 views

help with performing operations on matrix parts inside a loop

I would like to translate the following code into Mathematica language but am having some trouble doing so: ...
0
votes
2answers
119 views

How to read value from a plot

Let's say I have plot. What is the command for getting the specific value at a point that I choose? Exampe: Plot like --I-. command(1) = 0 command(2) = 0, command(3)=1, command(4)=0
2
votes
1answer
65 views

How to set linear constraints on elements of a long list

I would like to minimize over quite large set of $a_i$ some function $f(a_1,a_2,\dots,a_n)$ subject to a set of $k$ linear constraints: $$ i=1\dots n: \quad a_i\ge0;\quad j=1\dots k:\quad\sum_{i=1}^n ...
2
votes
0answers
25 views

How to abort evaluation when clicking CancelButton in program with Dialog boxes?

If you run the following code you will notice when Cancelbutton is clicked, the output is an $Abort, but it continues running the following cells, I tried different things but no one works. I want to ...
0
votes
0answers
52 views

How to improve the parallelization of the following piece of code?

I'm new to the parallelization of code. I have a code that takes so much time to run and I'm trying to parallelize it. I'm copying below the piece that I need to improve. Can someone help with the ...
18
votes
1answer
199 views

Confirm vs Throw

Version 12.2 brought us Confirm and Enclose. What's the point? Is it just that it's a slightly more convenient syntax than ...
1
vote
2answers
121 views

Plotting Riemann Prime Counting

I want to program the formula in this page. Didn't take me long to get to my code below. But 1) it only plots the prime counting function and 2) evaluation takes incredibly long. How can I fix it? <...
3
votes
2answers
108 views

Best way to write a system of conditions

Quite simply, I am wondering if there is a cleaner (and more easily altered) way to write a system of conditions for an evaluation. For instance, suppose I have something like (I don't think a real ...
0
votes
2answers
34 views

Define-Produce function from a loop?

Consider the following code: ...
4
votes
3answers
170 views

generating lists of integers with constraint

Suppose you have the list $a=(k_1,k_2,k_3, \dots, k_N)$ such that $k_{i}\; (i=1,\ldots, N)$ are non negative integers. How can you get all the lists that satisfy the constraint $\sum_{i=1}^{N} k_{i}=M$...
12
votes
1answer
616 views

Programming a bishop's move on a grid

I am simplifying the question i need too work with, any advice on how to proceed for each step would be appreciated(no answers) I have a square grid of size a by b, inside the cell is an object that ...
5
votes
3answers
269 views

Looking for a notebook with the “glowing gasket” [closed]

I look for an implementation of the "glowing gasket" as described in the book "Indra's Pearls" in Mathematica. Here is a typical image: I did a look at Wolfram Demonstrations and ...
0
votes
1answer
126 views

How to solve following equation with multiple random variables

How can I program the following problem into Mathematica? $X,Y$ are i.i.d. Exponentially distributed random variables. Such that $X\sim\exp(1), \ Y\sim\exp(1)$. All other variables are positive real-...
1
vote
1answer
47 views

How to change the joining code?

I'm trying to combine 4 matrices but I'm getting a matrix of 4x16 not 8x8. A={{1,2},{2,3}}; B={{2,1},{3,3}}; DD={{4,5},{6,37}}; EE={{12,11},{13,14}}; :
1
vote
1answer
82 views

How to know when FindRoot[] fails to find a solution

My problem is very simple. Assume you have this piece of code : FindRoot[{x^2 + 1 == 0}, {x, -1, 1}] Now, this code returns ...
6
votes
1answer
141 views

Mimicking named arguments, a good idea? (programming style)

With respect to code readability I like the possibility to use keyword (named) arguments. I would like to have this under Mathematica, but AFAIK there is no native support for that. By example, to ...
1
vote
1answer
54 views

XOR combination between the bits of a string

Given an integer n, we can construct $2^n$ strings of length n. We can take the first element for each of these strings and create a list. In total 'n' such lists are possible. But now I need to ...
2
votes
1answer
44 views

Simplest way to check if optional parameter is omitted?

Suppose I have a function f[x_,y___]:= (code) and I want my code to check whether the parameter y is omitted. What's the easiest ...
1
vote
1answer
70 views

How avoid to memorize 1 billion useless data

I am a newbie Mathematica user I wanted to know, using brute force, how many non empty triplets $\{A_1,A_2,A_3\}$ of subsets of $$AA=\{1,2,3,4,5,6,7,8,9,10\}$$ satisfy the conditions $A_1\cup A_2\cup ...
0
votes
1answer
98 views

How do I see output for intermediate steps in a program? [closed]

I understand Mathematica programs to be a series of steps enclosed in simple parentheses. Each 'step' ends in a semicolon except for the last. The program output is then the output of the last step ...
0
votes
1answer
62 views

TogglerBar linked to Buttons that override input list

This question is about extending the solution provided here. How do I allow the user to change x outside the TogglerGrid function so that the TogglerBar shows the new values of x? I've set it up ...
3
votes
1answer
129 views

Need help with a linear programming problem

Consider $1\rightarrow{}x+2y\leq{500}$, $2\rightarrow{}2x +y \leq{520}$, $3\rightarrow{}2x+5y \leq{}1200$, $4\rightarrow{}x \geq{}0$, $5\rightarrow{}y \geq{}0$, The above is the set of inequalities ...
4
votes
0answers
118 views

What is the best coding style for performance?

The problem of Mathematica is there are too many ways to perform one task. That leads to confusion for new users because the performances of these methods are so different. I'm learning the best ...
6
votes
4answers
300 views

Converting an equation with units to one without units

I may have a solution but it is not slick! See below Doing numerical calculations with functions that use units can be very slow. I would like to have a function or operator that can turn my functions ...
0
votes
1answer
106 views

Exporting a multi excel files into one file

Suppose a list of students'grades in excel: "excel files" I want to write a program separate student grades to their respective classes and then write them in one excel file. What should I ...
0
votes
1answer
47 views

How to load a function from an external notebook that creates a docked cells toolbar?

I coded a compact toolbar in its own file nbtaskmate.nb for various common notebook tasks which I would like to load onto any notebook I might have open with a ...
5
votes
3answers
387 views

Stacked time series plot

I have multiple time series measured in different units. I want to have a stacked plot of my data with a common x-axis. An example would be here. For illustration, here is my data. ...
3
votes
1answer
47 views

Generating set of equations of varying length and solving them

I am trying to generate a set of equations that describe a change in density for each variable in the form of: $\frac{dX_i}{dt}=X_i(r_i-s_iX_i+\sum_{j=1,j\neq i}^na_{ij}X_j)$ where n is the number of ...
11
votes
2answers
372 views

How can I plot my financial indicator below a trading chart (produced by TradingChart) with the same x-axis?

Since this question was not answered, I would like to ask again with some additional information. I have a trading chart saved as chart1 as follows. ...

1
2 3 4 5
30