Tag Info

Hot answers tagged

11

Taking user5601's suggestion to do a little demo, I quickly whipped this up as an example of ProcessLink being used to do non-trivial communication between Mathematica and an external program, but with much less ceremony than using ProcessLink or MathLink. Let's take this little Go program: package main import "net/http" import "bufio" import "os" import ...


6

d1 = RandomVariate[ExponentialDistribution[.1], 2000]; d2 = RandomVariate[TruncatedDistribution[{20, 40}, ExponentialDistribution[.1]], 2000]; GraphicsColumn[{Histogram[d1, {1}, PlotRange -> {{0, 72}, {0, 250}}], Histogram[d2, {1}, PlotRange -> {{0, 72}, {0, 250}}, AxesOrigin -> {0, 0}]}]


6

One can define the conjugate and use it to construct the rational and radical coefficients (rat and rad resp.). Just as PowerExpand assumes bases are positive reals, conj[x] will be correct only if the symbolic variables and functions in an expression x represent rational numbers. conj[x_] := x /. Sqrt[2] -> -Sqrt[2]; rat[x_] := (x + conj[x])/2; rad[x_] ...


6

You can use ToNumberField: 2/3 + 1/4 Sqrt[2] ToNumberField[%, Sqrt[2]] which produces AlgebraicNumber[Sqrt[2], {2/3, 1/4}]


5

Mathematica code in general does not look like C code. I will provide a solution that may not help you short term if your goal is to learn about For, If and Switch statements. However if you go through each step and look up all the functions in the documentation then you will learn several things about Mathematica which are useful to know. ...


5

When Mathematica encounters an incomplete expression, it will just continue reading on the next line. For example, 1 + 2 is the same as 1+2. However, 1 + 2 is interpreted as two distinct inputs, 1 (which is complete) and +2. As you mentioned, \ can also be used at the end of the line to indicate continuation. So 1 \ + 2 is also equivalent to ...


4

Edit: I accidentally used Identity when I meant Sequence, but on reflection I believe Join @@ is better. My two standard methods are Apply: (reference Formatting text through pattern matching and Quick multiple selections from a list) Join @@ Reap[Sow[1]][[2]] Join @@ Reap[Null][[2]] Join @@ Reap[Sow /@ {1, 2, {}}][[2]] {1} {} {1, 2, {}} And ...


2

Try some thing like this: SetDirectory[dir]; files = FileNames["*.txt"]; Export[newdir <> "\\" <> files[[#]], Cases[Import[files[[#]]], {1, _}][[;; , 2]]] & /@ Range[Length[files]]


2

Wolfram has replaced MathLink with WSTP, but both their document and tool related to this are outofdated. Because of this bug, nobody could successfully compile the "addtwo.c" demo in "C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\Links\WSTP\DeveloperKit\Windows\WSTPExamples\addtwo", even if he followed the instruction in "WSTP Development ...


1

In MATLAB, the command kmeans() also returns distances from data points to centroids, i.e. [idx,C,sumd,D] = kmeans(___) So, a more complete answer would be: kmeans[data_, k_] := Module[ {idx , cntr, result, noOfCols, noOfRows, c, i, y, sumd, dist}, idx = ClusteringComponents[data, k, 1, Method -> "KMeans", "DistanceFunction" -> ...


1

Actually, the program as written works fine, but you forgot to define n. I tried it for n=3 and obtained credible results: n = 3; f[x_?VectorQ] := n^2*ListConvolve[{1, -2, 1}, x, {2, 2}, {1, 0}]; ans = X /. NDSolve[{X'[t] == f[X[t]], X[0] == (Range[n - 1]/n)^10}, X, {t, 0, 0.25}][[1]] and, for instance, ans[.2] gives {0.252832,0.584738}. Best wishes ...


1

group = (Switch[#, 1 | 2 | 3, 1, _, 2 ] &); GatherBy[testdat, group[ #[[1]] ] & ] { {{1, a11, b11}, {1, a12, b12}, {2, a21, b21}, {3, a31, b31}, {2, a22, b22}, {3, a32, b32}, {3, a33, b33}}, {{4, a41, b41}, {4, a42, b42}, {5, a51, b51}, {6, a61, b61}, {6, a62, b62}, {5, a52, b52}, ...


1

One of the ways is the following: GatherBy[testdat, Position[{{1, 2}, {3, 4, 5}, 6}, #[[1]]][[1]] &] (* {{{1, a11, b11}, {1, a12, b12}}, {{2, a21, b21}, {2, a22, b22}}, {{3, a31, b31}, {3, a32, b32}, {3, a33, b33}}, {{4, a41, b41}, {4, a42, b42}}, {{5, a51, b51}, {5, a52, b52}, {5, a53, b53}, {5, b54, b54}}, {{6, a61, b61}, {6, a62, b62}}} *) For ...


1

Cases[testdat, {Alternatives[##], __}] & @@@ {{1, 2}, {3, 4, 5}, {6}} Update: based on your comment you can do it using two methods: prepare your index before index=Range[1,6,2] and then Cases[testdat, {Alternatives[##], __}] & @@@ {index} you can also do it like this: Flatten[Table[Cases[testdat, {i, __}], {i, 1, 6, 2}], 1]


1

@Kuba is correct Flatten[Reap[r][[2]], 1] is a good substitute for Reap[r][[2,1]] when cannot guarantee at least one Sow the line above should be substituted with reapList[r_] := Flatten[Reap[r][[2]], 1] because the __ is unnecessary as well


1

If I understand you correctly you are looking for Divisors of N. try this: dd=Divisors[250] (*{1, 2, 5, 10, 25, 50, 125, 250}*) All results have R=0. Mod[250, dd] (*{0, 0, 0, 0, 0, 0, 0, 0}*)



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