Hot answers tagged

20

I've always wondered about the scalability of MathLink (now officially "Wolfram Symbolic Transfer Protocol"). This is the protocol used by Mathematica to communicate between the front end and the kernel, and the basis of the Parallel` package. It has quite low bandwidth and high latency relative to, for example, MPI libraries. I also wonder how many MathLink ...


18

SatisfiableQ has three methods: "BDD": converts the expression to a BDD (binary decision diagram), "SAT": uses the Minisat library, "TREE": a branch-and-bound method based on the expression tree. SatisfiabilityCount counts instances by converting the expression to a BDD, so its timing should be close to SatisfiableQ with the "BDD" method (counting ...


14

You can accomplish this using DeleteDuplicatesBy, by first taking your two input lists and making a matrix out of them, and then deleting the rows where the last element (the element that came from myNewList) is a duplicate. Then you transpose back and assign the sublists of the reduced matrix to the new lists you want. {myListDuplicatesDeleted, ...


14

dat = {0.71, 0.685, 0.16, 0.82, 0.73, 0.44, 0.89, 0.02, 0.47, 0.65}; Module[{t = 0}, Split[dat, (t += #) <= 1 || (t = 0) &] ] {{0.71, 0.685}, {0.16, 0.82, 0.73}, {0.44, 0.89}, {0.02, 0.47, 0.65}} Credit to Simon Woods for getting me to think about using Or in applications like this. Performance I decided to make an attempt at a higher ...


13

I am sure you can easily install also Linux on it and then you could contact Vladyslav Shtabovenko, the current maintainer of FeynCalc (https://github.com/vsht) and ask him about hard problems in High Energy Physics he would like to benchmark on such a King-Kong machine. Either him or somebody else could also provide you with more complicated examples of ...


13

Select[ IntegerPartitions[24, {8}, Range[5]], #.# == 86 & ] {{5, 5, 4, 2, 2, 2, 2, 2}, {5, 5, 3, 3, 3, 2, 2, 1}, {5, 4, 4, 4, 2, 2, 2, 1}, {5, 4, 4, 3, 3, 3, 1, 1}, {4, 4, 4, 4, 4, 2, 1, 1}} Slightly more general approach (in case where IntegerPartitions is not what we need): ClearAll[ar, a]; ar = Array[a, 8] ar /. Solve[Flatten@{ ...


12

Comparable to other results: Lookup[Thread[myNewList -> myList], DeleteDuplicates@myNewList] {1,3} Seems most appropriate (fast and concise) for the OP task (V.10): Values@AssociationThread[myNewList -> myList] {1,2} P.S. Learned the general idea from this post of Mr.Wizard. Addenum: Some notes concerning speed. @Mr.Wizard suggested ...


11

I would choose a problem that exploits the unique power of Mathematica, in particular the natural functions involving graph theory, symbolic math, graphics, and the high compute power you have available. So I would choose some image recognition and clustering problem such as: Take some large number of images ($\sim\!\!10^8$) and perform deep learning on ...


11

Using NestWhile seems to work well z[n_, c_] := NestWhile[(#^2 + c) &, c, Abs[#] <= 2 &, 1, n]; ContourPlot[Abs[z[iter, x + I*y]] == 2, {x, -.00001, .00001}, {y, .99999, 1.00001}, MaxRecursion -> 5] // AbsoluteTiming producing the plot in the question in about 10 seconds, as opposed to 180 seconds for the code in the question.


9

I think this is pretty clearly a case where Outer is simply overloaded with an optimized definition for Plus. A look at the Trace shows that there are no intermediate steps: Trace[ Outer[Plus, Range[3], Range[3]], TraceInternal -> True ] {{Range[3], {1, 2, 3}}, {Range[3], {1, 2, 3}}, Outer[Plus, {1, 2, 3}, {1, 2, 3}], {{2, 3, 4}, {3, 4, 5}, {4, ...


8

I have a method that's more accurate, but I'm not sure how robust it is in the end. But maybe some of my tricks are useful for you. My first step to make the problem easier is to try to remove the perspective. If the pipes are all (more or less) vertical lines in the image, I can use image processing filters with anisotropic filter sizes, i.e. filters that ...


8

4 TB memory? N-body of course; dir = 2; T = 10; k = 1; l = 10; n = l^3; v = 6; q[i_] := (-1)^i m[i_] := RandomReal[5] Rem[o_] := l (o - IntegerPart[o]) eqns = Table[ {D[Subscript[r, i][t], {t, 2}] m[i] == Sum[ Normalize[Subscript[r, i][t] - Subscript[r, j][t]] k q[ i] q[j]/(Subscript[r, i][t] - Subscript[r, ...


7

You can do it probably most efficiently in compiled code, if you're not too concerned about precision. Here you can use the listability of compiled functions over tensor arguments. Your function is basically the Mandelbrot iteration: mandelbrot = Compile[{{c, _Complex, 0}, {d, _Integer, 0}}, Block[{i = 0, z = c}, While[Abs[z] < 2.0 && i < ...


7

The following is ten times faster. The remaining time is mostly consumed while evaluating your function, so there may be some optimization window there. point1[j_] := Join[x[[;; j]], w[[j + 1 ;;]]]; point2[j_] := Join[x[[;; j - 1]], w[[j ;;]]]; max = 11; fx = f[x]; β = SparseArray[{{i_, i_} -> -1/100}, {size, size}]; Do[{ w = x + β.fx; T = ...


7

A slightly different approach with Reduce (or Solve) We define: matC = Range[5]; (* the list of the integers from which we build a list *) Let us use a vector to indicate the multiplicities of any integer: matX = Array[ x, 5 ]; (* @Kuba: that's more concise, indeed *) So x[1] will tell us how many times $1$ appears a possible solution. We can then ...


6

Your Func is far beyond best. As mentioned before, currently you'd better set effort on learning to code in "Mathematica-style" rather than try to relieve the slowness (which is mostly caused by bad coding in my view) with Compile. Here's an uncompiled function that's about 300 times faster than your Func: func = Function[{l0, l1, l2, ll3}, Module[{l3 = ...


6

The following is approx 30 times faster: (imageData = Flatten@ImageData@image; mean = Mean@imageData; nonzeroPixels = Total@Unitize@imageData; zeroPixels = Length@imageData - nonzeroPixels; stdDeviation = StandardDeviation@imageData) // Timing


6

You can also use FindHamiltonianCycle. To convert Hamiltonian path problem to Hamiltonian cycle problem, just add one vertex and connect it to all other vertices. After that just run FindHamiltonianCycle[g, All] For example, countHamiltonianPaths[g_] := Length[ FindHamiltonianCycle[ AdjacencyGraph[PadRight[AdjacencyMatrix[g], (VertexCount[g] + ...


5

This might be faster (you will need to install libpng): Needs["CCompilerDriver`"] loadpng$source=" // Load a PNG image from a file into memory. // Placed into the public domain by Mark Adler, January 24, 2016 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <setjmp.h> #include ...


5

Module[{f}, f[x_] := (f[x] = 0; 1); Pick[myList, f /@ myNewList, 1]] {1, 3}


5

The following two expressions are equivalent. Table[RandomReal[], {10^8}]; // AbsoluteTiming {7.99593, Null} RandomReal[1., 10^8]; // AbsoluteTiming {1.20604, Null} The second expression shows the advantage of RandomReal over Random. Edit Another consideration is the generator used. For example, when the Mersenne twister is specified, there is not ...


4

General idea A general rule of thumb to reduce the time and memory needed for a computation is to use inexact (floating-point) numerics instead of exact or symbolic approaches, and to use it early. Of course, one might worry about accuracy, but there's no guarantee that feeding an exact, symbolic problem to FindRoot will be more accurate than feeding an ...


4

I realize this is an old question, but I recently had the same issue and have come across (link to google groups question) what I think is a cleaner solution. I don't want to take credit for coming up with that solution, but I thought it would be helpful to add it to this site. I'll use a simple example function to demonstrate. f[x_] := f[x] = x ...


4

Here's a quick helper function to do so. It first finds the position of each $1$ in the original matrix using Position; it then selects $n$ of these positions randomly using RandomSample and replaces their value with $-1$ using ReplacePart: Clear[flipsome] flipsome[matrix_?MatrixQ, n_Integer] := ReplacePart[matrix, RandomSample[Position[matrix, 1], n] ...


4

I love the brevity and elegance of garej's second method and I would probably Accept it for that reason if nothing else, were this my Question. Nevertheless it seems that using the position method referenced in the question is actually faster. positionDuplicates[list_] := GatherBy[Range @ Length @ list, list[[#]] &] a = RandomReal[1, 1*^6]; b = ...


4

The more I've thought about this question, the more my answer (above) has changed. Now my answer is this: Assuming there is a high-bandwidth connection to the Wolfram server, choose a problem that relies on Mathematica's superior handling of curated data. Create an enormous problem that relies on curated financial data, geographic data, biological data, ...


4

To take advantage of that kind of memory, you really want to do some parallel processing. Mathematica's parallel processing focuses on data-parallelism, or more simply, embarrassingly parallel problems. So you might try out various Monte Carlo simulations. I don't recommend trying to reproduce full MPI functionality with LinkCreate et al. In the case ...


4

This is not really very efficient but here goes. We can create a rational function that is effectively a generating function in three variables, one to force 8 factors, one to force a sum e1ual to 24, one to force a sum of squares equal to 86. The other parameters just keep track of what factors get used in a coefficient. vals = Range[5]; aa = Array[a, 5]; ...


4

Since Sum holds its arguments, it ends up computing Mean[x] and Mean[y] in every step in the sum. Try Total[(x - Mean[x])(y - Mean[y])]/Total[(x - Mean[x])^2] which uses vectorized operations. In general, Sum is most suited to compute sums of symbolic quantities, sums with symbolic limits etc. Total or Tr is much faster for numeric stuff.


4

What I am most interested in is described in Oleksandr's answer. Here's something else I would also try: There are several built-in functions that take advantage of parallelization without any special settings (and without using the Parallel tools framework). I would like to know how well these scale to a high number of cores. Examples: Matrix ...



Only top voted, non community-wiki answers of a minimum length are eligible