Hot answers tagged

21

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 ...


20

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 ...


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

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 ...


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, ...


9

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, ...


8

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 ...


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 ...


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

Here's my take at making a function as fast as possible. main = Module[{idxs = sub[Accumulate@#]}, Internal`PartitionRagged[#, idxs]] &; sub = Compile[{{list, _Real, 1}}, Block[{i, l = Length[list], ref = 1., bag = Internal`Bag[{0}]}, For[i = 1, i <= l, i++, If[list[[i]] >= ref || i == l, Internal`StuffBag[bag, i]; ref = ...


7

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 ...


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] + ...


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

Internal`PartitionRagged uses Accumulate internally to generate a list of positions from the sub-list lengths, then MapThread and Take to extract the corresponding elements from the array. You can check the internal definition with Needs["GeneralUtilities`"]; PrintDefinitions[Internal`PartitionRagged] The reason for pointing this out is that answers which ...


6

(1) Convolution with Plus and Times can be done via FFT. (2) The overall speed complexity cannot be less than the size-of-result complexity. Point (1) might help to explain why the standard ListConvolve is fast under most circumstances. Point (2) on the examples in this post should help to explain why LC2 is likely to be slow. To make this clear we can ...


5

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


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

f[x_, y_] := Module[{new}, If[Total[new = Append[x, y]] >= 1, Sow[new]; {}, new]] Reap[Fold[f, {}, dat]][[2, 1]]


5

OK, a partial answer without considering h["TestDataTable",All]: In the plot it is much faster to move the pars inside CDF. (Computing CDF numerically vs. symbolically, I guess.) AbsoluteTiming[d1 = CDF[f[a, b, c, p] /. pars, x];] (* <1s *) AbsoluteTiming[d2 = CDF[f[a, b, c, p], x] /. pars;] (* 14s *) And it is faster to first compute the functions ...


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

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

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

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

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 ...



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