Tag Info

Hot answers tagged

14

I know this thread is old and that Leonid provided a great solution, but I'd like to present a bit different approach. My approach is inspired by JavaScript. There are no classes, just "pure objects". To directly answer requirements from question Support instantiation, inheritance and polymorphism. Objects are fully dynamic, everything can be changed in ...


10

In this answer, I will use the Functional Paradigm to deal with triangular recursive formula in a uniform manner. For the triangular recursive formula $$T_k^{(n)}=f(T_{k-1}^{(n)},T_{k-1}^{(n+1)})$$ In general, $f(x)=a x+b$, so the triangular recurisive formula can be denoted as below: $$T_k^{(n)}=\alpha(k,n) T_{k-1}^{(n)}+\beta(k,n)T_{k-1}^{(n+1)}$$ ...


10

$$\mathbb{P} \big(\sum_{i=1}^{m} (A_i + S_i) \le L < \sum_{i=1}^{m+1} (A_i + S_i) \big) = \frac{\mu ^m (2 \lambda +\mu )}{2^{m+1} (\lambda +\mu )^{m+1}}$$ Observing: d1 = TransformedDistribution[ a + s, {a \[Distributed] ExponentialDistribution[λ], s \[Distributed] ExponentialDistribution[μ]}] (* ...


10

Here are some debugging ideas and a possible workaround. Note that for v10, I am using the Wolfram Programming Cloud, which as far as I can tell, is reproducing your results. Version specific specific problem Using this code: $HistoryLength = 0; i = Import["ExampleData/CTengine.tiff", "Image3D"]; oldMem = MemoryInUse[]; GaussianFilter[i, 5]; ...


7

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

Overall, I think your main confusion stems from mixing up different the programming paradigms and syntax in Mathematica. The way Functional and Rule & Patterns based programming works is different. To quote the Documentation: The Wolfram Language stands out from traditional computer languages in supporting many programming paradigms. In ...


5

As per @Szalbocs suggestion: v10 alternatives are RegionMember and Element, but the latter is unreasonably slow. A drop in alternative RegionMember[reg] returns a RegionMemberFunction[...] that can be applied repeatedly to different points. (* Memoizing the RegionMemberFunction[...] for a given polygon *) inPolyQHelper[poly_] := inPolyQHelper[poly] ...


4

Just another way (if I have interpreted correctly): fun[m_] := With[{p = Position[m, -1], w = 1 - 2 IdentityMatrix[Length@m]}, If[p == {}, w, Extract[w, p]]] So, Column[# -> fun@# & /@ Tuples[{-1, 1}, 3]]


4

I am not sure whether the intention is vectors with all positive entries. If not then potential pairs (a,-a) will also be division by zero. Here is another implementation of formula, removing zero denominators: cd[u_, v_] := Module[{pos, us, vs}, pos = Position[u + v, _?(# != 0 &)]; us = Extract[u, pos]; vs = Extract[v, pos]; Total[(us - ...


4

Here's a straightforward implementation of the formulas posted in the question. Clear[t, s, c, r]; t[f_, {a_, b_}, n_] := (b - a)/(2 n) * ReplacePart[ConstantArray[2, n + 1], {1 -> 1, n + 1 -> 1}] . (f /@ Rescale[Range[0, n], {0, n}, {a,b}]) s[f_, {a_, b_}, n_] := (4^1 t[f, {a, b}, 2 n] - t[f, {a, b}, n])/(4^1 - 1); c[f_, {a_, b_}, n_] := (4^2 ...


4

This is all covered in the documentation. To answer your specific questions: Yes, just call Needs or Get more than once. Yes, by setting up contexts correctly Yes, but in Mathematica they are called Contexts. The issue is likely to be that you did not use a standard package structure to write your packages, including the Begin["Private`"]. See the ...


4

In Mathematica you can iterate over i != j with Do[<code>, {i, n}, {j, Drop[Range[n], {i}]}] In a sense it really does the actual iteration desired. The following iterates over all pairs {i, j}, although <code> is executed only for i != j. Do[If[i != j, <code>], {i, n}, {j, n}] The difference in speed is minimal but measurable: ...


3

Here's general approach, which I primarily show in order to give an answer that includes the Neville-Aitken algorithm. It peculiarly works from the bottom of the triangle up, that is $T_k^{(n)}$ or t[k, n] are generated in the order shown in the table: One of the distinctions to make clear is whether the function $f$ in the recursion ...


3

Nice to meet you, Mr Shu. Bug fix first. Your function doesn't work under desired precision because: Table[trapezium[func, 2^i, {a, b}], {i, 0., iter}] Changing it to Table[trapezium[func, 2^i, {a, b}], {i, 0, iter}] still doesn't fix the problem, because all the numbers taking part in the calculation all have infinity precision then. Adding a ...


3

Here is old HW assignment. The code is not very functional at all. I even use Break[] in there (OMG!) , but I get same result as the table in the book shows. I put them side-by-side with the magic of cut/paste: Code: nmaRomberg[c_] := Module[{len = Length[c], r, k = 1, f, j, i}, r = Table[0, {len}, {len}]; r[[All, 1]] = c; Do[ k++; f = ...


3

I post this to illustrate some of the ways you could do this task. I have voted for Simon's answer, however, as it addresses the 'why' (the point of the question and the most important point). f[x_?PrimeQ] := Framed[x]; f[x_] := x; g[x_] := Framed[x] /; PrimeQ@x; g[x_] := x; h[x_] := Piecewise[{{Framed[x], PrimeQ@x}, {x, True}}]; j[x_] := Which[PrimeQ@x, ...


2

The legacy documentation says: In version 10 MathLink was replaced with WSTP. The equivalent flag is now -wstp. If I run MathKernel.exe from the command line without any flags I get this: However if I run MathKernel.exe -wstp (equivalent to the old -mathlink flag I believe) I get only:


2

RandomFunction produces a TemporalData object. The action of Histogram on TemporalData is to just do a histogram of the values (ignoring time stamps). td = RandomFunction[QueueingProcess[3, 5], {0, 15}]; GraphicsRow[{Histogram[td], Histogram[td["Values"]]}] Thus, you can either wrap your data in TemporalData or just make a histogram of the values ...


2

Using Defer or HoldForm in the first argument and StandardForm in the last argument of the outer ToString: ToString[Defer@Flatten[Array[StringJoin["(", ToString[(-1)^#1], ",", ToString[(-1)^#2], ")"] &, {2, 2}]], StandardForm] (* ...


2

plotOne[g_Graph] := Module[{probs, purgedTab, r = Range@Max@VertexDegree@g}, probs = {#, Probability[x == #, x \[Distributed] VertexDegree[g]]} & /@ r; purgedTab = DeleteCases[probs, {x_, y_} /; x y == 0]; ListLinePlot[Log@purgedTab, Filling -> Axis, Mesh -> Full, MeshStyle -> Directive[PointSize[Large], Black], ...


2

Following your clarification and inspired by ubpdqn's answer I propose: f[a:_List] := Pick[1 + 2 DiagonalMatrix[a], a, -1] f[a:{1..}] := f[-a] Test: f @ {-1, -1, 1} f @ {1, 1, 1} {{-1, 1, 1}, {1, -1, 1}} {{-1, 1, 1}, {1, -1, 1}, {1, 1, -1}}


1

I'm hardly sure that I have understood your question fully, but does this give you the result that you want? Table[Probability[x == k, x \[Distributed] VertexDegree[j]], {j, allgraphs}, {k, Max[VertexDegree[j]]}] You need to specify the j iterator first, as the specification of k depends on j. On a more general note, I would say that if I want to ...


1

I'm afraid I don't know what realized covariance means. Perhaps the easiest solution is to use RLink and directly use the R implementation. Here are some links to the documentation to get you started. http://reference.wolfram.com/language/RLink/guide/RLink.html http://reference.wolfram.com/language/RLink/tutorial/UsingRLink.html


1

You can use Sum and a Which statement to test which Log expression to calculate. Also, remember that equality is checked with ==, not =. Sum[ Which[ x1[[i,1,2]] == 2, Log[ x1[[i,1,1]] ]^2, x1[[i,1,2]] == 1, Log[ x1[[i,1,1]] ] * Log[ x1[[i,2,1]] ] ],{i,4} ]


1

This is not actually an explanation, just some insights. In order to see whats going here I modified your code in the following way: Remove[f]; SetAttributes[f, {Flat, OneIdentity}]; f[0, i_] := i /; (Print[{0, i}]; True) f[i_, 0] := i /; (Print[{i, 0}]; True) f[i_, i_] := 0 /; (Print[{i, i}]; True) f[i_, j_] := 6 - i - j /; (Print[{i, j}]; True) For ...


1

a = {-1, -1, 1} ones = Table[1, {Length[a]}]; ReplacePart[ones, # -> -1] & /@ Position[a, -1] (* {{-1, 1, 1}, {1, -1, 1}} *) a = {1, 1, 1}; ones = Table[1, {Length[a]}]; ReplacePart[ones, # -> -1] & /@ Position[a, 1] (* {{-1, 1, 1}, {1, -1, 1}, {1, 1, -1}} *) Update to combine into one function: foo[a_List] := Module[{ones = Table[1, ...


1

For instance, if I have a process and it first spends 2 minutes in state 1, then 200 minutes in state 2 and 10 minutes in state 0: {{0,0},{2,1}, {202, 2}, {212,0}} I would want the time be included in the calculation of the relative frequency as opposed to the number of occurences. Based on this description, I can suggest the following approach: ...


1

Better late than never, but the error in the python script appears to be due to the format of the current packet. Using your routine (with k = kernel): >>> k.putfunction("Prime",1) >>> k.putinteger(10) >>> k.flush() >>> k.nextpacket() 8 8 is a RETURNPKT, but what is the kernel returning? >>> k.getnext() 34 ...


1

in response to your comment, I wrote a small example using map: First create some data: bodies = 4; createBody[size_] := Module[ {rp := RandomReal[{-1.00 size, 1.00 size}], rv := RandomReal[{-0.07 size, 0.07 size}]}, {{rp, rp, rp}, {rv, rv, rv}, 1./bodies}] data = Map[createBody, Range[bodies]] Output: {{{0.353151, -0.362113, -0.177183}, ...



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