wolfram-mathematica

Using Position correctly

≯℡__Kan透↙ 提交于 2019-12-19 18:21:34
问题 In the mma help docs for Position, the following is listed under "Possible Issues" In[1]:= Position[Range[-1, 1, 0.05], 0.1] Out[1]= {} There is no explanation given though. Why does this happen? So if I really need to find the position of 0.1 in Range[-1,1,0.05] , how do I do it? 回答1: It is a numeric precision issue: 0.1 in the Range is not internally the same as 0.1 typed in. The normal way to resolve this is to compare with Equal rather than the implicit SameQ . Position[Range[-1, 1, 0.05]

Matlab vs Mathematica, eigenvectors?

南笙酒味 提交于 2019-12-19 10:08:33
问题 function H = calcHyperlinkMatrix(M) [r c] = size(M); H = zeros(r,c); for i=1:r, for j=1:c, if (M(j,i) == 1) colsum = sum(M,2); H(i,j) = 1 / colsum(j); end; end; end; H function V = pageRank(M) [V D] = eigs(M,1); V function R = google(links) R = pageRank(calcHyperlinkMatrix(links)); R M=[[0 1 1 0 0 0 0 0];[0 0 0 1 0 0 0 0];[0 1 0 0 1 0 0 0];[0 1 0 0 1 1 0 0]; [0 0 0 0 0 1 1 1];[0 0 0 0 0 0 0 1];[1 0 0 0 1 0 0 1];[0 0 0 0 0 1 1 0];] google(M) ans = -0.1400 -0.1576 -0.0700 -0.1576 -0.2276 -0

How to create a notebook with a properly formatted expression

∥☆過路亽.° 提交于 2019-12-19 10:03:02
问题 I have a Mathematica expression generated by another program, which I would like to open in a notebook, properly formatted. For instance, the other program generates this: Plot[{Exp[x],Interpolation[Table[{k/5,Exp[(k-1/2)/5]},{k,0,5}], InterpolationOrder->0][x]},{x,0,1},Filling->{1->{{2},{Yellow,Orange}}}, PlotLabel->Style["Formatting",Blue,FontFamily->"Courier"]] The text is written into a file, crudely suffixed ".nb", and launched, and the expression opens in a notebook without formatting.

How to create a notebook with a properly formatted expression

假装没事ソ 提交于 2019-12-19 10:01:48
问题 I have a Mathematica expression generated by another program, which I would like to open in a notebook, properly formatted. For instance, the other program generates this: Plot[{Exp[x],Interpolation[Table[{k/5,Exp[(k-1/2)/5]},{k,0,5}], InterpolationOrder->0][x]},{x,0,1},Filling->{1->{{2},{Yellow,Orange}}}, PlotLabel->Style["Formatting",Blue,FontFamily->"Courier"]] The text is written into a file, crudely suffixed ".nb", and launched, and the expression opens in a notebook without formatting.

Overloading Set[a, b] (a = b)

不羁岁月 提交于 2019-12-19 09:41:53
问题 I would like to overload Mathematica's Set function (=), which turns out to be too tricky for me (see following code example). I successfully overloaded other functions (e.g. Reverse in the code example). Any suggestions? In[17]:= ClearAll[struct]; In[18]:= var1=struct[{1,2}] Out[18]= struct[{1,2}] In[19]:= Reverse@var1 Out[19]= struct[{1,2}] In[20]:= Head[var1] Out[20]= struct In[21]:= struct/:Reverse[stuff_struct]:=struct[Reverse@stuff[[1]]] In[22]:= Reverse@var1 Out[22]= struct[{2,1}] In

Using `With` with a list of `Rules` - but without affecting the normal behaviour of `With`

和自甴很熟 提交于 2019-12-19 07:53:19
问题 Say I have a list of Rules rules = {a -> b, c -> d}; which I use throughout a notebook. Then, at one point, it makes sense to want the rules to apply before any other evaluations take place in an expression . Normally if you want something like this you would use In[2]:= With[{a=b,c=d}, expr[a,b,c,d]] Out[2]= expr[b, b, d, d] How can I take rules and insert it into the first argument of With ? Edit Both Some solutions fail do all that I was looking for - but I should have emphasised this

How to Autonumber Cell Tags in Mathematica Notebooks?

╄→尐↘猪︶ㄣ 提交于 2019-12-19 03:57:00
问题 I tried to follow the directions on autonumbering cells in a Mathematica-8 notebook, here http://reference.wolfram.com/mathematica/tutorial/AutomaticNumbering.html I created a tiny notebook with four text cells foo qux blancmange bar Placing the cursor just before foo , I then used the Insert menu, Automatic numbering item, giving me the Create Automatic Numbering Object dialog box. I chose in the Counter dropdown then item Text , clicked This counter object radio button, and Huzzah! got the

package import problem in mathematica

耗尽温柔 提交于 2019-12-19 03:23:07
问题 In mathematica (I am using mma 5.0 ( guess pretty old)), if I type the following as one line: Needs["Graphics`Master`"]; Animate[Plot[Sin[n x], {x, 0, 2 Pi}, Axes -> False], {n, 1, 6, 1}] I then got a lot of errors/warnings. But if I type them in separately, it is working fine. How to make it work in one code block? Thanks! 回答1: As belisarius points out, your question as it stands is a bit v5-centric. The problem, however, still exists in current versions. As an example Needs["Combinatorica`"

Finding frequency of range of numbers in Mathematica

本秂侑毒 提交于 2019-12-19 03:08:10
问题 Given a list of numbers in Mathematica, how would I extract from that list the total number of numbers between numbers a and b that I specify? 回答1: The most direct way is simply: Count[data, x_ /; a <= x <= b] There are however much faster ways for most data, this one thanks to Carl Woll: Tr@Unitize@Clip[data, {a, b}, {0, 0}] Carl Woll's method is particularly fast, but as yoda pointed out, it fails if your list contains zeros, and your range also straddles zero. Here is another method from

Memoization in OCaml?

情到浓时终转凉″ 提交于 2019-12-18 17:13:35
问题 It is possible to improve "raw" Fibonacci recursive procedure Fib[n_] := If[n < 2, n, Fib[n - 1] + Fib[n - 2]] with Fib[n_] := Fib[n] = If[n < 2, n, Fib[n - 1] + Fib[n - 2]] in Wolfram Mathematica. First version will suffer from exponential explosion while second one will not since Mathematica will see repeating function calls in expression and memoize (reuse) them. Is it possible to do the same in OCaml? How to improve let rec fib n = if n<2 then n else fib (n-1) + fib (n-2);; in the same