wolfram-mathematica

Is Put - Get cycle in Mathematica always deterministic?

蓝咒 提交于 2019-12-05 18:45:41
问题 In Mathematica as in other systems of computer math the numbers are internally stored in binary form. However when exporting them with such functions as Put and PutAppend they are converted into approximate decimals. When you import them back with such functions as Get they are restored from this approximate decimal representation to binary form. The question is whether the recovered number is always identical to the original binary number and, if not always, in which cases it is not and how

Python decimal.Decimal precision doesn't match wolfram alpha's

▼魔方 西西 提交于 2019-12-05 18:32:48
I have the following python code: In [1]: import decimal In [2]: decimal.getcontext().prec = 80 In [3]: (1-decimal.Decimal('0.002'))**5 Out[3]: Decimal('0.990039920079968') Shouldn't it match 0.99003992007996799440405766290496103465557098388671875 according to this http://www.wolframalpha.com/input/?i=SetPrecision%5B%281+-+0.002%29%5E5%2C+80%5D ? Wolfram alpha is actually wrong here. (1 - 0.002) ** 5 is exactly 0.990039920079968 . You can verify that by simply assessing that there are 15 digits after the . , which matches 5 * 3 , 3 being the number of digits after the . in the expression (1 -

How to improve the performance of this piece of code?

China☆狼群 提交于 2019-12-05 18:05:53
I'm trying to learn a bit of Julia, after reading the manual for several hours, I wrote the following piece of code: ie = 200; ez = zeros(ie + 1); hy = zeros(ie); fdtd1d (steps)= for n in 1:steps for i in 2:ie ez[i]+= (hy[i] - hy[i-1]) end ez[1]= sin(n/10) for i in 1:ie hy[i]+= (ez[i+1]- ez[i]) end end @time fdtd1d(10000); elapsed time: 2.283153795 seconds (239659044 bytes allocated) I believe it's under optimizing, because it's much slower than the corresponding Mathematica version: ie = 200; ez = ConstantArray[0., {ie + 1}]; hy = ConstantArray[0., {ie}]; fdtd1d = Compile[{{steps}}, Module[

How to shade a plot in Mathematica

丶灬走出姿态 提交于 2019-12-05 18:00:47
I want to generate a plot like the following I am not sure how to generate a shading even though I can get the frame done. I'd like to know the general approach to shade certain areas in a plot in Mathematica. Please help. Thank you. Perhaps you are looking for RegionPlot ? RegionPlot[(-1 + x)^2 + (-1 + y)^2 < 1 && x^2 + (-1 + y)^2 < 1 && (-1 + x)^2 + y^2 < 1 && x^2 + y^2 < 1, {x, 0, 1}, {y, 0, 1}] belisarius Note the use of op_ in the following (only one set of equations for the curves and the intersection!): t[op_] :=Reduce[op[(x - #[[1]])^2 + (y - #[[2]])^2, 1], y] & /@ Tuples[{0, 1}, 2] tx

Problems interpreting input cell box expressions

和自甴很熟 提交于 2019-12-05 16:04:09
问题 How do I convert an arbitrary box specification extracted from a cell expression into an input expression? This came up as a problem with my answer to Save Mathematica code in FullForm syntax. In that context, pattern matching was being used to extract box specifications from notebook expressions read using Import . I thought that ToExpression or MakeExpression would do the job of box interpretation, but in some circumstances they do not. Consider an input cell that contains the expression:

An efficient code to determine if a set is a subset of another set

杀马特。学长 韩版系。学妹 提交于 2019-12-05 15:35:40
问题 I am looking for an efficient way to determine if a set is a subset of another set in Matlab or Mathematica. Example: Set A = [1 2 3 4] Set B = [4 3] Set C = [3 4 1] Set D = [4 3 2 1] The output should be: Set A Sets B and C belong to set A because A contains all of their elements, therefore, they can be deleted (the order of elements in a set doesn't matter). Set D has the same elements as set A and since set A precedes set D, I would like to simply keep set A and delete set D. So there are

How to kill slave kernel securely?

前提是你 提交于 2019-12-05 14:41:00
LinkClose[link] "does not necessarily terminate the program at the other end of the connection" as it is said in the Documentation. Is there a way to kill the process of the slave kernel securely? EDIT: In really I need a function in Mathematica that returns only when the process of the slave kernel has already killed and its memory has already released. Both LinkInterrupt[link, 1] and LinkClose[link] do not wait while the slave kernel exits. At this moment the only such function is seemed to be killProc[procID] function I had showed in one of answers at this page. But is there a built-in

An unexpected behavior of PatternTest in Mathematica

醉酒当歌 提交于 2019-12-05 14:01:37
问题 I am working on toy problems to help me assimilate the idea of pattern matching in Mathematica. The following code does not behave as I expected, and I could not figure out what is wrong with my understanding of PatternTest. MatchQ[{2, 1, 2, 5}, {x__?(FromDigits[{#}] > 3 &), y__}] I expected this piece of code to check if the list {2,1,2,5} can be written as two consecutive (non-empty) sequences such that the integer we get from the first sequence is greater than 3. Since {Sequence[2,1]

Mathematica: How to prevent evaluation of In[-1]?

爱⌒轻易说出口 提交于 2019-12-05 13:35:07
When I'm trying to get last input expression in Wolfram Mathematica 5.2 using In[-1] I get evaluated output: In[1]:= On[In] 2 + 2 In[-1] Out[2]= 4 During evaluation of In[1]:= In::trace: In[-1] --> In[$Line-1]. >> During evaluation of In[1]:= In::trace: In[$Line-1] --> In[2]. >> During evaluation of In[1]:= In::trace: In[2] --> 2+2. >> Out[3]= 4 How can I get the input expression in unevaluated form? You could try InString[] In[1]:= 2 + 2 Out[1]= 4 In[2]:= InString[-1] Out[2]= "\\(2 + 2\\)" Alexey Popkov Here is another way: In[1]:= 2 + 2 With[{line = $Line - 1}, HoldForm[In[line]] /.

When does Mathematica create a new Symbol?

爷,独闯天下 提交于 2019-12-05 13:18:50
Good day, I thought earlier that Mathematica creates new symbols in the current $Context at the stage of converting of the input string (that is assigned to InString ) to input expression (that is assigned to In ). But one simple example has broken this explanation: In[1]:= ?f During evaluation of In[1]:= Information::notfound: Symbol f not found. >> In[2]:= Names["`*"] Out[2]= {} In[3]:= DownValues[In]//First InString[1] Names["`*"] Out[3]= HoldPattern[In[1]]:>Information[f,LongForm->False] Out[4]= \(? f\) Out[5]= {} You can see that there is no symbol f in the $ContextPath although it is