wolfram-mathematica

how to automatically load user-defined functions in mathematica

最后都变了- 提交于 2019-12-03 03:57:54
I have a bunch of user-defined functions that are frequently used in mathematica. I wonder if I can store them in separate files and mathematica will load them on start and treat them as built-in functions, so that I don't have to repeat the definitions whenever I create a new .nb file. Something similar to functions in Matlab... Thanks! You can create a package in $UserBaseDirectory/Autoload. This will be loaded at Kernel initialization time. Your package should have a Kernel/init.m file MyPackage/Kernel/init.m Reference documentation on Mathematica packages: http://reference.wolfram.com

Load a mathematica package from within a package

。_饼干妹妹 提交于 2019-12-03 03:54:59
问题 I have more or less the following setting. In ~/path/to/my/packages I have two packages package1.m and package2.m . Each package's outline is, for example, the following: BeginPackage["package1`"] Unprotect@@Names["package1`*"]; ClearAll@@Names["package1`*"]; Begin["`Private`"] vecNorm[vec_?VectorQ]:=Module[{},Return[Sqrt[vec.vec]]]; End[] Protect@@Names["package1`*"]; EndPackage[] Now, my problem is that I want to use vecNorm defined in package1.m in package2.m . How can I load (safely)

How to make a grid of plots with a single pair of FrameLabels?

自古美人都是妖i 提交于 2019-12-03 03:54:38
问题 What is the simplest way to create a row/column/grid of plots, with the whole grid having a single FrameLabel? I need something similar to this: p := ListPlot[RandomInteger[10, 5], Joined -> True, Axes -> False, Frame -> True, PlotRange -> {0, 11}, FrameLabel -> {"horizontal", None}, AspectRatio -> 1] GraphicsRow[{Show[p, FrameLabel -> {"horizontal", "vertical"}], p, p}] For a row format, it could have one or multiple horizontal labels, but only one vertical one. Issues to consider: Vertical

how to animate 3d plot given a rotation axis in mathematics

怎甘沉沦 提交于 2019-12-03 03:47:02
If given a rotation axis normalized, such as {1/Sqrt[3],1/Sqrt[3],1/Sqrt[3]}, and a 3d plot, for example, z[x_, y_] := Exp[-(Sqrt[x^2 + y^2]/Power[4, (3)^-1]) + Power[4, (3)^-1]*Sqrt[1/2*(Sqrt[x^2 + y^2] + x)]]; Plot3D[2*z[x, y], {x, -5, 5}, {y, -5, 5}] I want to create an animation for this plot about the axis {1/Sqrt[3],1/Sqrt[3],1/Sqrt[3]} (could be any other arbitary one), and then export it as an animated gif. Would anyone please help? Many thanks. Edit I also left out one degree of freedom in specifying the rotation. Could any one please help, if also given the coordinate of a point

Non-standard evaluation and PackedArray

元气小坏坏 提交于 2019-12-03 03:42:34
I have earlier asked how to make allTrue[{x,list},test] function that protects the placeholder symbol x from evaluation in current context in the same way as Table[expr,{x,...}] protects x The recipe that I ended up using failed intermittently, and I found the problem down to be caused by automatic conversion of lists to PackedArrays. Here's a failing example SetAttributes[allTrue, HoldAll]; allTrue[{var_, lis_}, expr_] := LengthWhile[lis, TrueQ[ReleaseHold[Hold[expr] /. HoldPattern[var] -> #]] &] == Length[lis]; allTrue[{y, Developer`ToPackedArray[{1, 1, 1}]}, y > 0] I want allTrue[{x,{1,2,3}

What would a minimal example for a choropleth map in Mathematica look like?

混江龙づ霸主 提交于 2019-12-03 03:28:58
What would a minimal example for a choropleth map in Mathematica look like? I can read in a ESRI Shapefile using Import , but do not know how to work with the imported result. Graphics[ { ColorData["ThermometerColors"][ Rescale[CountryData[#, "GDPPerCapita"], {100, 50000}] ] /. HoldPattern[Blend[___]] -> Yellow, CountryData[#, "Polygon"] } & /@ CountryData[] ] And why the replacement? If there are no data of the required type for a given country CountryData returns Missing["NotAvailable"], causing ColorData , and its underlying Blend function not to return a specific RGB value. I replace this

chaos game for DNA sequences

只愿长相守 提交于 2019-12-03 03:25:01
I have tried the mathematica code for making the chaos game for DNA sequences posted in this address: http://facstaff.unca.edu/mcmcclur/blog/GeneCGR.html which is like this: genome = Import["c:\data\sequence.fasta", "Sequence"]; genome = StringReplace[ToString[genome], {"{" -> "", "}" -> ""}]; chars = StringCases[genome, "G" | "C" | "T" | "A"]; f[x_, "A"] := x/2; f[x_, "T"] := x/2 + {1/2, 0}; f[x_, "G"] := x/2 + {1/2, 1/2}; f[x_, "C"] := x/2 + {0, 1/2}; pts = FoldList[f, {0.5, 0.5}, chars]; Graphics[{PointSize[Tiny], Point[pts]}] the fasta sequence that I have is just a sequence of letters

Way to deal with large data files in Wolfram Mathematica

若如初见. 提交于 2019-12-03 03:19:36
问题 I wonder if there exists way to work with large files in Mathematica ? Currently I have a file about 500Mb with table data. Import["data.txt","Table"]; What is alternate way? 回答1: Use OpenRead["file"] which gives you an InputStream object on which you can use Read[stream]. Depending on the formatting of the data file you may need to set custom option values in Read[] for RecordSeparators . Example: In[1]:= str = OpenRead["ExampleData/USConstitution.txt"] Out[1]= InputStream["ExampleData

How to decrease file size of exported plots while keeping labels sharp

血红的双手。 提交于 2019-12-03 03:17:59
When exporting rather complicated plots (especially ListDensityPlot ) as a PDF or EPS (for publication, for example), the resulting file size can be quite large. For example: data = Flatten[Table[{f0, f, Exp[-(f - f0)^2/25^2]}, {f0, 500, 700, 5}, {f, 300, 900}], 1]; plot=ListDensityPlot[data,PlotRange->{Automatic,Automatic,{0,1}},InterpolationOrder->0] This example data set is on the order of the size I typically work with. When I export using Export["C:\\test.pdf", plot] , it generates a PDF file 23.9MB in size. If I instead try Export["C:\\test1.pdf", Rasterize[plot]] it is far smaller, but

Subscripted variables

守給你的承諾、 提交于 2019-12-03 02:54:16
问题 Is there any way to force Mathematica to treat subscripted variables independently of their unsubscripted counterparts? More specifically. Say, I have the following definitions: Subscript[b, 1] = {{1, 2}} Subscript[b, 2] = {{3, 4}} b = Join[Subscript[b, 1], Subscript[b, 2]] Now when I use Subscript[b, 1] Mathematica will substitute it with Subscript[{{1, 2}, {3, 4}},1] when I want these to be three independent values, so changing b will not affect Subscript[b, ..]. Is it possible? 回答1: In an