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.

learn more… | top users | synonyms

0
votes
1answer
31 views

Is it possible to use FormObjects and FormFunction in a stand-alone notebook?

I have been looking at the FormObject and FormFunction. Can FormObject and ...
0
votes
0answers
20 views

Solving (one variable) Linear Equation by Dividing Slope by Constants Accumulation? [migrated]

I've been Google searching for this, and apparently my Googlefu is not strong today, or I'm doing something wrong. I have been told that it is possible to solve a one variable linear equation by first ...
0
votes
0answers
28 views

Evaluate notebook using text-based interface

I am trying to evaluate a notebook using a text based interface so that it can be submitted to a computing cluster. I have seen this stackexchange page and attempted to use ...
1
vote
1answer
35 views
0
votes
0answers
35 views

What does a period do when used in a test argument for Which? [closed]

I was looking at an example for error reporting. I noticed that a . appears in the tests of a Which clause. The first test in the Which clause below is: ...
1
vote
2answers
87 views

How to find total distance covered when I have a list of GeoPositions?

So I have a list of GeoPositions and I need to find the distance covered in that order? I know I can find the distance from each city and then add them all up but I was wondering if there is any ...
0
votes
0answers
29 views

Debug package with command line arguments

I'm creating a package that takes as arguments input from the command line using $ScriptCommandLine. What's unclear to me is how to best debug this package. How do others perform these tasks? I ...
0
votes
1answer
48 views

How to properly use conditionals inside NDSolve

Currently I am working with Mathematica to learn the program, and I'm confused what exactly is wrong with this code. ...
6
votes
1answer
61 views

Completion in terminal?

I know that it is possible to run Mathematica in Terminal. But I didn't find a way to get completion work in Terminal. I mean completion by ...
0
votes
1answer
71 views

How do I find the nearest city out of a list of cities?

I am trying to find the next closest city to city 1 and want to use a certain radius to check what is the closest city I'm trying to create an algorithm for a TSP. Does anyone know how to implement ...
1
vote
1answer
63 views

Multiply out product of sums

For a specific quantum mechanical problem I need to multiply out operators in order to calculate a trace by hand. For example I need a Hamiltonian squared with $H^2$. The Hamiltonian contains of a few ...
3
votes
2answers
310 views

How to force Return[] to return from an arbitrary point (like in C++)?

I need to test some complicated conditions to calculate a Return value. But MMa doesn't let you Return[] from anywhere the way C++ does. The structure of my Module is similar to this: ...
-1
votes
1answer
99 views

Implementing the Golden Section Rule

I am attempting to write a code which brackets the minimum of a unimodal function, using the Golden Section Method Below. I am have serious issues debugging my program though, any help will be ...
-1
votes
1answer
75 views

Trying to write my own Greedy/ Nearest Neighbor Algorithm

So I'm trying to write my own function and compare it to the results I have from using FindShortestTour. Does anyone know how to find the distance from each city to each of the others without ...
2
votes
1answer
66 views

Block works when directly used but not when used in a function

I want to calculate a derivative such as: $\delta f=\sum_{i=A,B} \sum_{k=1,2,3} \frac{\partial f}{\partial a_k^i}$. Since $ a_k^i$ are assigned values other places in the code, I have used ...
3
votes
1answer
154 views

How do I create an array by applying a function to the index of each element?

I am looking for something like matrix(m, n, f), but for Mathematica rather than MuPAD, and for arrays rather than matrices. That is, I'm looking for a(n ...
5
votes
4answers
266 views

Efficient way to sum all the primes below $N$ million in Mathematica

Today, I saw a problem described as below: Sum all primes below $N$ million At the beginning, I feel it is rather simple to calculate it in Mathematica ...
0
votes
2answers
162 views

FindShortestTour to solve Traveling Salesman Problem

I'm trying to write my own code to find the shortest route but I'm lost on how or where to start. Does anyone have any idea how the FindShortestTour function works? What type of algorithm or structure ...
0
votes
1answer
70 views

Eigen energies of 2D time independent Schrödinger equation

Firstly, I am aware that a similar question of this type was asked, but it was not helpful as it was only one dimensional (so please don't mark my question as a duplicate) The trouble I am having is ...
0
votes
2answers
61 views

Diophantine and odd

need to resolve this Diophantine equation, but with the condition that a, b, c belong to the set of odd numbers from 1-51, as I write this condition in the code ...
-1
votes
0answers
25 views

How could I speed up a code including a Module with many functions

I have implemented a big formula through the mathematica function Module. Within this module a lot of definitions are made (i.e. function[x_]:=). The code are working well but it is too slow. I am ...
5
votes
1answer
128 views

How is LHS = RHS; … ; RHS (nontrivially) different from … ; RHS

I'm trying to understand how Mathematica thinks about the results of Set. Suppose we perform LHS = RHS. I'm focusing on when ...
2
votes
1answer
101 views

How to define the Bernstein function like the built-in BernsteinBasis?

We know the Bernstein function defined as below: $$B_{n,i}(u)=\binom n i u^i(1-u)^{n-i}$$ And we define $B_{n,i}(u)=0$ when $i<0 $ or $i>n$ In addition, the first derivative of $B_{n,i}(u)$ ...
0
votes
1answer
60 views

cross point of intersecting lines [closed]

What is the easiest way to find the crossing point of two intersecting lines passing lets say through points line1 = {p1,p2}, line2 = {p3,p4}?
2
votes
1answer
97 views

Separate output screen; clearing (cleaning) output screen

Is there a way to split the screen so that the code is on top visible, and execution of code look at the bottom? Is there any way to clean the screen from previous runs, so that the result of ...
7
votes
3answers
202 views

How to speed up auxilary DoolittleDecomposite function?

The Doolittle Decomposition algorithm as below: $$A=LU$$ where $A= \begin{bmatrix} a_{11} & a_{12} & \cdots a_{1n}\\ a_{21} & a_{22} & \cdots a_{2n}\\ \vdots & \vdots & \vdots ...
0
votes
0answers
31 views

$MessageList inside and outside Block[]

Why does 1/0 $MessageList return ComplexInfinity whereas ...
0
votes
1answer
28 views

How to combine two or more groups while selecting form the unbalanced panel data if the number of obs is less than a scalar in certain groups

I have an unbalanced panel data. I want to calculate group statistics for each group. However, if ith group has less than K obs, then I want to combine ith and (i+1)th groups to calculate group ...
1
vote
0answers
40 views

How to use RunProcess to run python interpreter

In a related question How to use RunProcess to import external function? @taliesin provides a very nice answer illustrating using RunProcess to launch a GO http server. Has anyone, or does anyone ...
4
votes
0answers
101 views

Delete duplicates by a criteria

Given that I have a set of points as below ...
8
votes
2answers
166 views

Relaxation of Mass-Spring System with Pinned Masses

Problem Assume that four masses are connected to each other by springs while two of them are pinned down at their initial positions. Find the position of masses when system is at its minimum ...
4
votes
1answer
60 views

subtleties about variable shadowing in Block[]

Let' s consider this packege : BeginPackage["myProgram`"]; myProgramF::usage = "arg + 1"; Begin["`Private`"]; myProgramF[arg_: 0] := arg + 1; End[]; EndPackage[]; ...
4
votes
4answers
268 views

Spiral roots, how?

I found this picture on the net, as I can play in mathematica
1
vote
1answer
35 views

Implicit region is not “valid” in ParamatricNDSolveValue function?

I am trying to solve a geometric problem with relation to my Schrodinger equation and its boundaries. Here is my code: ...
3
votes
2answers
140 views

How can I create a 3D representation of an object with known coordinates and adjacency table in mathematica?

Basically I want to create an object with specified coordinates and adjacency table, like in page two of this article. I'm completely new to using Mathematica, any help would be great! EDIT: So I ...
3
votes
1answer
209 views

How to download pictures from webpage automatically?

Today, I read a good web page about love which owns many beautiful pictures. It is fussy for me to download every picture hand-by-hand, so I'd like to use Mathematica to do this case automatically. I ...
0
votes
0answers
33 views

Retrieve rendered graphics via WSTP

I am planning on using Mathematica as calculation- and rendering backend for an application, i.e.: Call Mathematica via WSTP and by using EvaluatePacket have it ...
2
votes
3answers
136 views

How to extract both first and last rows of each group from unbalanced panel data

This is a follow up question form my last question click here. I have unbalanced panel data such as : ...
3
votes
1answer
106 views

How can I overload a function with multiple bracket-slots so f[a][b] and f[a] can coexist?

Maybe this is not even possible: I want to create a function f that can have two input brackets like: f[a_][b_:1]:= a*b and ...
6
votes
1answer
194 views

How can a function know in which function it was evaluated?

Hello my second question here might be very hard. I am asking for something "magical", if i define a function g and i call it inside a function f, how can g have information about the outer function ...
2
votes
2answers
64 views

Need help updating an old Mathematica worksheet

I found a notebook that symbolically applies the multiple time scales method to any differential equations. I don't like just using other people's code so I am going through the author's work line by ...
5
votes
2answers
341 views

Find permutations with constraints without using Permutations

EDIT: To clarify, the bottleneck right now is available RAM, so any answer should keep that in mind (I cannot store all T! lists of length ...
0
votes
0answers
42 views

Trying to write a program to find an approximate harmonic function on a given graph using relation method

So given an undirected graph with two vertices that have fixed values assigned to them, a harmonic function is a function that assigns values to all other vertices such that the value assigned to each ...
0
votes
1answer
81 views

How to improve this function that selects parameters from arbitrary distribution using Recursion?

Below, I have defined a function using Which to select model parameters based upon various conditions. My questions are: Is there any danger in writing this function using Recursion? Is there a more ...
1
vote
1answer
98 views

Perceptron: Do loop not returning a value

I am trying to write a function that finds a simple best fit line for some data (Perceptron algorithm). Sadly it returns no output at all. Here's the code below: ...
2
votes
2answers
118 views

How to plot points with error circles on map with latitude/longitude grid

I am currently working on a project to model the location of icebergs in the northwest atlantic. When an iceberg is sighted, an error circle is plotted around it (with a bivariate normal probability ...
4
votes
0answers
62 views

Using mathlink/WSTP with fortran

I would like to access Mathematica functions from within a Fortran program. I am unsure how to start (which is probably related to my lack of up-to-date Fortran knowledge). Is it necessary to write a ...
1
vote
1answer
76 views

Hypothesis Testing and Formatting Output

I am trying to write a function that tests hypotheses and gives me a nice table but I can't. I need forum's help. For example, I have the following data ...
5
votes
0answers
92 views

How to construct custom operators with precedence?

I need to combine data structures in operations analogous to addition, subtraction, multiplication and division (and more). I need more than one operation of each type, i.e. more than one method of ...
2
votes
1answer
113 views

How can I invoke the solution of NDSolve to determine a parameter in my equation just inside NDSlove?

I am trying to solve a differential equation by NDSlove for $h(x,t)$. It reads $$h_t=h_{xx}-V_h-\lambda(t)$$ where $V_h$ is a given function of $h(x,t)$ denoted by ...