wolfram-mathematica

Using Array and Table Functions in Mathematica. Which is best when

試著忘記壹切 提交于 2019-12-20 08:42:42
问题 I have been mostly a Table functions user in mathematica. However I have noticed that in several examples where I used Array instead of Table to express the same result, it ran markedly faster, especially as the dimension of table grew larger. So my question is this: When speed of execution is the primary concern, when is it most appropriate to use table? What explains this difference? My guess is that because Arrays assume a functional relationship between the items in the list, it stores

Mathematica: How to obtain data points plotted by plot command?

安稳与你 提交于 2019-12-20 08:35:22
问题 When plotting a function using Plot, I would like to obtain the set of data points plotted by the Plot command. For instance, how can I obtain the list of points {t,f} Plot uses in the following simple example? f = Sin[t] Plot[f, {t, 0, 10}] I tried using a method of appending values to a list, shown on page 4 of Numerical1.ps (Numerical Computation in Mathematica) by Jerry B. Keiper, http://library.wolfram.com/infocenter/Conferences/4687/ as follows: f = Sin[t] flist={} Plot[f, {t, 0, 10},

How to find line where error occurred in Mathematica notebook?

六月ゝ 毕业季﹏ 提交于 2019-12-20 08:18:23
问题 I have a Mathematica file called myUsefulFunctions.m containing, for example, a function called mySuperUsefulFunction. Suppose I call mySuperUsefulFunction in a notebook and get the following error: Part::pspec: Part specification #1 is neither an integer nor a list of integers. >> Is there a way to find the line in myUsefulFunctions.m where this error occurred? 回答1: I don't know of a way to find the line in the file, which I assume was read without error. You can however use Trace and

Getting the ith permutation of a list

不想你离开。 提交于 2019-12-20 07:49:14
问题 I need a fortran code that calculates the ith permutation of a given list {1,2,3,...,n}, without computing all the permutations, that are n!. Is there anyone that can help me? Thank you in advance. 回答1: I solved my problem. In the following I show you both the Mathematica and Fortran codes I implemented. If you have any advise for improve them, do not esitate to comment. MATHEMATICA (example: 10° permutation of {1,2,3,4,5}) n = 5; i = 10; p = Range[n]; ithPermutation = p; Do[f = (n - k)!; d =

Density plot in Matlab

邮差的信 提交于 2019-12-20 07:36:15
问题 What is the Matlab's comparable function to Mathematica's ListDensityPlot ? For example ListDensityPlot[Table[Sin[x y/100], {x, -50, 50}, {y, -50, 50}], DataRange -> {{-50, 50}, {-50, 50}}] will produce Thank you. 回答1: Matlab is different than Mathematica in the sense of vectorized computations vs symbolic computation. You could do what you'd like by defining a grid of spatial samples for x & y and then sample your function accordingly. For example: dx = 0.1; dy = 0.1; x = -50:dx:50; y = -50

Mathematica: subscript simplification under noncommutative multiplication

落爺英雄遲暮 提交于 2019-12-20 05:25:10
问题 Using Subscript[variable, integer] in Mathematica 7.0+, I have expressions of the following form: a_-4 ** b_1 ** a_-4 ** b_-4 ** a_1 ** c_-4 ** c_1 ** c_5 I would like to simplify this expression. Rules: * Variables with the same subscript to don't commute, * variables with different subscripts do commute. I need a way to simplify the expression and combine like terms (if possible); the output should be something like: (a_-4)^2 ** b_-4 ** c_-4 ** b_1 ** a_1 ** c_1 ** c_5 The most important

How to make an analog of InString[]?

风格不统一 提交于 2019-12-20 02:42:50
问题 I have discovered that InString[] does not work in MathLink mode when sending input with EnterExpressionPacket header. So I need to define my own function that returns previous input line. One way I have developed here does not work in some cases: In[1]:= Unevaluated[2 + 2] With[{line = $Line - 1}, HoldForm[In[line]]] /. (DownValues[In]) Out[1]= Unevaluated[2 + 2] Out[2]= 2 + 2 This is because RuleDelayed has no HoldAllComplete attribute. Adding this attribute makes this OK: In[1]:= Unprotect

Calculating expectation for a custom distribution in Mathematica

房东的猫 提交于 2019-12-20 01:41:18
问题 This question builds on the great answers I got on an earlier question: Can one extend the functionality of PDF, CDF, FindDistributionParameters etc in Mathematica? To start I have PDFs and CDFs for two custom distributions: nlDist and dplDist as you can see from the code dplDist builds upon nlDist. nlDist /: PDF[nlDist[alpha_, beta_, mu_, sigma_], x_] := (1/(2*(alpha + beta)))*alpha* beta*(E^(alpha*(mu + (alpha*sigma^2)/2 - x))* Erfc[(mu + alpha*sigma^2 - x)/(Sqrt[2]*sigma)] + E^(beta*(-mu +

Specifics of usage and internal work of *Set* functions

喜夏-厌秋 提交于 2019-12-19 19:45:19
问题 I just noticed one undocumented feature of internal work of *Set* functions in Mathematica . Consider: In[1]:= a := (Print["!"]; a =.; 5); a[b] = 2; DownValues[a] During evaluation of In[1]:= ! Out[3]= {HoldPattern[a[b]] :> 2} but In[4]:= a := (Print["!"]; a =.; 5); a[1] = 2; DownValues[a] During evaluation of In[4]:= ! During evaluation of In[4]:= Set::write: Tag Integer in 5[1] is Protected. >> Out[6]= {HoldPattern[a[b]] :> 2} What is the reason for this difference? Why a is evaluated

Using Position correctly

纵饮孤独 提交于 2019-12-19 18:23:52
问题 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]