Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

17
votes
5answers
1k views

Sisyphus Random Walk

A Sisyphus Random Walk evolves as follows: With probability (r) you advance the position of the walker by +1. With probability (1-r) the walker resets at x0 = 0. To simulate the probability of reset ...
1
vote
0answers
68 views

How can we make an algorithm with the A* method in Mathematica with 5 different flight routes? [duplicate]

I need to find the shortest path with the A* algorithm using Mathematica. I have 5 different flight routes with coordinates. They start and end at the same point. Between every point there is a wind ...
1
vote
2answers
74 views

How to print chess board with array? [duplicate]

I am doing n-queen implementation with my own algorithm and I get solutions with Number.But,I want to get output with actual chess board. This is my piece of code. ...
-2
votes
2answers
88 views

What does this code mean? [closed]

Here solutions is array . I need to know why this code is written.Please help me. ...
2
votes
1answer
80 views

How to use external Mathematica Code? [duplicate]

I have a Mathematica Notebook with a function that i would like to be able to access from another Notebook without having to have the first Notebook open. The functions calls other functions in the ...
0
votes
1answer
71 views

Transforming a conic to it's standard form

Suppose a conic is given.I need to transform it to standard form.So i tried like this: ...
1
vote
1answer
61 views

how to sort a list without build-in function

I want to sort a list like below.Without using the build-in function so I tried this: ...
-1
votes
1answer
91 views

Solving two coupled PDEs with the pdetoode function posted on this site [closed]

I want to solve two coupled PDEs with pdetoode function: feq = {D[ϕ, t] + U*D[ϕ, y] - D[ϕ, {y, 2}] - (s0 - 1/20*y^2)*ϕ == 0} and ...
5
votes
0answers
162 views

How to Write Stand-alone Mathematica Projects/Programs

I wish to write several stand-alone projects/programs using Mathematica because of its easy-to-use graphics. I could use Python for this, but Mathematica has built-in graphics that would be better ...
1
vote
0answers
39 views

Making a function $ f(n) $ tangential to another function $ g(n) $ by choosing appropriate constants

I have two functions $ f(n) = 1/(1+n) + b, (n \geq 0,b \geq 0) $ and $ g(n) = pn^2 + qn + r $. Assuming that I know the value of $ p, q $, and $ r $, I am trying to find a value for the constant $ b $ ...
1
vote
3answers
77 views

How to randomly generate Tree or Nested List of Function in Mathematica?

In detail, I want to generate randomly something like Divide[Add[X,Times[X,Y]],Times[X,3]] or {Divide,{Add,X,{Times{X,Y}}},{Times,X,3}} (Specified Function(# of argument is from 2 to 5)) + ...
-2
votes
2answers
146 views

Mathematica is not responding for very large numbers

So, I am having Mathematica 11.3 and trying to evaluate 2^1,000,000,000. When I am trying to show the output in its full form to manipulate the digits, Mathematica stops responding. I am having 32GB ...
0
votes
3answers
82 views

Check if number is in list

I have a nested list, but don't know how long the individual lists are. Sometimes they may be 2 numbers long sometimes 1000, so i need to check if theres is a number at position 6 but i doesn't ...
1
vote
0answers
62 views

Is there packages can help read, analyze or understand MMA codes? [closed]

Sometimes, I think that it's difficult to learn high level codes, or even when looking back on some long lines of codes I myself had written, espectially when lots of pure-functions was used, so I'm ...
2
votes
0answers
35 views

Fold/ NestList/ etc in MCMC loop for optimisation

I am trying to simplify and optimise an old loop code I wrote. Since Fold and NestWhile use the results of the last iteration in the new iteration, I would like to use them in order to get rid of Do ...
9
votes
3answers
1k views

Speed up code to numerically simulate a game of coin tossing

Description I would like to simulate n games in which two players toss one coin each at the same time: If you toss a tail you will receive two tokens, if you toss ...
0
votes
1answer
110 views

Why can”t I get a smooth curve?

I used "Table" to calculate and plot the following complicated function. First, we choose an initial value for $G$ and other parameters, but at later times $G$ changes with function $I_a$. ...
1
vote
1answer
32 views

How to perform a set of commands specific amout of time?

I have 3 lines of code that does the Collatz Conjecture in mathematica. the code is the following: ...
2
votes
4answers
123 views

Subdivision of a line into n intervals of linearly changing length

This question is separate, but connected to my previous question on this site, that can be found here. Let's say that we have a line of length X (in this case X=5), defined by a list ...
1
vote
0answers
49 views

ConditionalExpression still resulting from Assuming a Module's local variables

I have a memoizing, recursive function whose MWE looks like this (the two fake inputs/ICs are from 'K' and 'Bfun2', the latter just a quick way to check the results coming out of the Residue.: ...
5
votes
1answer
68 views

Ignore message in `Check` without silencing

Check[expr,failexpr,checkedMessages] will evaluate expr and if it encounters any of the messages in ...
2
votes
2answers
240 views

How do I write this matrix?

The following code gives 20 data as output. How can I make a 5*4 matrix by that data list?
2
votes
1answer
36 views

How to select Input|Output Cell Groups in the Notebook?

I am trying to convert a Cell expression to an image. I created a new notebook and copied the cells that need to be rendered. ...
0
votes
0answers
42 views

magic square with prime numbers of any order

how could you create a script in Mathematica that generates magic squares of order n, but that the numbers that form the square are all primes I've looked at what's in other answers but I can not ...
1
vote
2answers
106 views
1
vote
2answers
57 views

How can I construct a matrix by programming in mathematica?

I want to write a matrix in Mathematica by programming. How can I write it? The matrix is : $ M= \begin{bmatrix} 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ...
0
votes
2answers
40 views

An Alternative Solution to a List Manipulation Example

Is there any way to write a code that has a function include Block[ ] and Do[ ] loop instead of my code? Here is my code: ...
2
votes
1answer
44 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)\ ,$$ ...
1
vote
1answer
43 views

How can I print only the lines for a file that contain consecutive digits?

I have a text file containing a list of numbers. I want to print only the lines that contain a number, the digits of which are consecutive digits. The input list: 123 321 567 765 124 568 The output ...
0
votes
0answers
20 views

FormCalc installation error in Mac

I have a Mac and I followed Feyninstall procedure, I have downloaded FeynArts and Looptools successfully. I have a problem with FormCalc. So checking install.log-FormCalc, the error is the following: ...
0
votes
2answers
54 views

Total does not give the output expected

Using the three matrices given below, I like to perform the operation stated in Quiet[...]/.{Interderminate->0}. ...
3
votes
3answers
215 views

What is wrong with Module? or me? [closed]

The following code does not give me an answer. It looks all is working when each command inside Module is individually run. But ...
2
votes
1answer
71 views

Recall the names of functions used in a long MMA code

Suppose you have a very long code in which many new functions are created. For efficient programming, very short and easily iterated function names are preferred. ...
0
votes
0answers
24 views

Symbol that constrains execution time relative to the computation that is performed

I'm looking for a way to implement a symbol that is like TimeConstrained in that it limits the execution time given to a certain operation, however does so on the ...
3
votes
3answers
123 views

Infinite While loop

I have an exercise where I need to input a multi-digit number and evaluate the square of the sum of its digits. I am new to Mathematica and I can't figure out why I'm getting an infinite loop. This is ...
3
votes
1answer
46 views

What is a good way to implement a versatile folder structure checking system?

I'm writing an 'application' (set of *.wl and *.wls files) that requires a given folder structure to function; I am not sure how this structure will eventually look like, because it changes as I ...
29
votes
3answers
513 views

Analogies to Help New Users

In many of the classes that I teach, I require students to learn the basics of Mathematica which we use throughout the semester to do computations and to submit homeworks (in notebook form). Some ...
3
votes
1answer
65 views

FindShortestTour finds very long tour

The documentation claims FindShortestTour works extremely well for hundreds or thousands of cities. I show an example set of 5 cities where it does an abominable job. I also show a couple of absurd ...
4
votes
1answer
214 views

Pythagorean quadruples

I am very interested in the problem that deals with the Pythagorean quadrruples, which are listed in rosettacode. Unfortunately, I have not been able to find any way to compute them with Mathematica. ...
10
votes
3answers
580 views

Can I use Compile to speed up InverseCDF?

I'm repeatedly issuing the command P = InverseCDF[NormalDistribution[0, 1], T] where T is either a 36- or 64-vector of real numbers (points in the unit ...
3
votes
3answers
359 views

How to create a fractal tree?

I want to create a fractal tree, using translation and rotation. Code below is working for one level, but when I try loop, it is not giving output. Code is given below: ...
4
votes
1answer
56 views

FindShortestTour shows erratic behaviour wrt 2nd argument

FindShortestTour behaves erratically. It should return a {distance, ordering} pair, and it does this for most values of its ...
0
votes
0answers
19 views

Why does the contour plot shows blank part? [duplicate]

when I try to plot some complicated functions coming from chi-square distribution, the plot gives me a result with a blank part. I try to narrow the spaced vertical bin using Log for multiple times ...
1
vote
1answer
78 views

What resources can I use to become familiar with high level syntax?

I was reading a question about dropping higher order terms and realized that I have a serous gap in knowledge when it comes to the finer points of mathematica computational programming. Are there any ...
3
votes
1answer
117 views

Taking a long time to produce output

In the following, I want to know b[1000], but it is taking a very long time even for b[30]. Please help ...
3
votes
1answer
256 views

How to calculate all possible resistances made from 5 distinct resistors in series and/or parallel? [closed]

Five distinct resistors resistors={1,2,3,4,5} are given. The objective is to find a list of all possible resistances obtained by configuring all these resistors in ...
0
votes
1answer
50 views

How to randomly generate a list of function calls from a list of args and a list of functions?

I want to generate a list of simple trigonometric questions in TeXForm, for example, as follows. ...
2
votes
2answers
70 views

Compound Statements and returning earlier results in () parentheses

I am trying to debug some script and need to Print[] out some results, sometimes before, and sometimes after, certain executions. These 3 trials exemplify my ...
4
votes
1answer
112 views

Jaro distance in MMA

I'm trying to program Jaro distance as requested by this page I've done the following code that works well for the next two pairs ("MARTHA", "MARHTA") and ("DIXON", "DICKSONX") but when I try with ("...
0
votes
2answers
182 views

Delay operator and Apply used when for applying function of a function

I define my own function like that: DelayL1t[(q_: 1) a_[t_]] := q Apply[a, {t - L1[t]/c}] DelayL2t[(q_: 1) a_[t_]] := q Apply[a, {t - L2[t]/c}] if I just do : ...