wolfram-mathematica

Reliable clean-up in Mathematica

女生的网名这么多〃 提交于 2019-11-29 23:17:56
For better or worse, Mathematica provides a wealth of constructs that allow you to do non-local transfers of control, including Return , Catch / Throw , Abort and Goto . However, these kinds of non-local transfers of control often conflict with writing robust programs that need to ensure that clean-up code (like closing streams) gets run. Many languages provide ways of ensuring that clean-up code gets run in a wide variety of circumstances; Java has its finally blocks, C++ has destructors, Common Lisp has UNWIND-PROTECT , and so on. In Mathematica, I don't know how to accomplish the same thing

Symbolic Matrices in Mathematica with unknown dimensions

被刻印的时光 ゝ 提交于 2019-11-29 23:07:23
Is there a way to do symbolic matrix algebra in Mathematica for matrices where the dimensions are unknown? For example, if I have an MxL matrix A and an LxN matrix B, I would like to be able to enter A.B And have it give me a matrix whose element ab[i,j] is given by Sum[a[i,l]*b[l,j],{l,1,L}] The problem I'm working on is like this one, but involves the product of 12 matrices, including the same matrix (and its transpose) repeated several times. It will probably be possible to simplify the values of the resulting matrix, but it's not obvious whether this is possible until after I do the

Profiling memory usage in Mathematica

[亡魂溺海] 提交于 2019-11-29 23:06:08
Is there any way to profile the mathkernel memory usage (down to individual variables) other than paying $$$ for their Eclipse plugin (mathematica workbench, iirc)? Right now I finish execution of a program that takes multiple GB's of ram, but the only things that are stored should be ~50MB of data at most, yet mathkernel.exe tends to hold onto ~1.5GB (basically, as much as Windows will give it). Is there any better way to get around this, other than saving the data I need and quitting the kernel every time? EDIT: I've just learned of the ByteCount function (which shows some disturbing results

How to create 2D (3D) animation in Wolfram Mathematica with the camera following the object?

蹲街弑〆低调 提交于 2019-11-29 22:38:46
问题 I have a graphical object which is moving along a trajectory. How can I make the camera follow the object? 回答1: Since the question asks about 2D, here's how you can emulate a camera in 2D Graphics. First, let's get the stackoverflow favicon.ico: so = First@Import["http://sstatic.net/stackoverflow/img/favicon.ico"] Well put this on top of some overlapping circles and make the "camera" follow the icon around by adjusting the PlotRange Manipulate[Graphics[{ Table[Circle[{j, 0}, i], {i, 0, 1, .1}

Why do BarChart graphics exported from Mathematica have pixelated text? Is there a workaround?

◇◆丶佛笑我妖孽 提交于 2019-11-29 22:27:19
问题 I've been showing off my fancy new graph formats to colleagues, but we have discovered that graphics based on BarChart have jagged text when exported as EMF, WMF, PDF etc. Line graphs based on ListLinePlot , DateListPlot etc do not have this problem. Short of Rasterize -ing every Export function automatically (it's for an application for end-users so they can't be expected to fiddle with it themselves), is there a workaround? It's a surprise because the documentation says: Since EMF supports

Inverse of Flatten in Mathematica?

扶醉桌前 提交于 2019-11-29 21:19:06
问题 What's the inverse of f[x_]:=Flatten[x] where x is Array with dimensions dims? 回答1: There is no built-in function, but it's pretty easy with a combination of Fold and Partition: In[47]:= x1 = RandomReal[{0, 1}, {3, 4, 5}]; In[48]:= dims = Dimensions[x1] Out[48]= {3, 4, 5} In[49]:= x2 = Fold[Partition, Flatten[x1], Most[Reverse[dims]]]; In[50]:= x1 == x2 Out[50]= True 回答2: You probably want Partition[] or one of its relatives. 来源: https://stackoverflow.com/questions/3807976/inverse-of-flatten

Setting up a “struct” in Mathematica safely

守給你的承諾、 提交于 2019-11-29 21:05:31
问题 The question on making a record like in Mathematica has been discussed in few places, such as Struct data type in Mathematica?. The problem with all these methods, is that one loses the ability, it seems, to do the specific extra check on each argument, as in when one does x_?NumericQ . My question is: Is there a way in Mathematica to make a record or a struct, and yet be able to use the checking as above on the individual elements? I am trying to settle down on one method to use as I am

Is there a way around using [[ and ]] for Part in Mathematica?

我们两清 提交于 2019-11-29 21:03:51
Is there a way to avoid having to do ⋮[[⋮ to obtain those great looking brackets for Part ? Is there a way for this to be done automatically after you ran a function or a definition ? abcd I have the following addition in /Applications/Mathematica.app/SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr which lets me enter double brackets with key combinations. You can do the same by modifying the file (where ever it is on your OS). I first learnt of this from Szabolcs's website here . He has other mathematica related stuff there that might be of help to you. The commands added

A fast implementation in Mathematica for Position2D

空扰寡人 提交于 2019-11-29 20:29:26
I'm looking for a fast implementation for the following, I'll call it Position2D for lack of a better term: Position2D[ matrix, sub_matrix ] which finds the locations of sub_matrix inside matrix and returns the upper left and lower right row/column of a match. For example, this: Position2D[{ {0, 1, 2, 3}, {1, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 6} }, { {2, 3}, {3, 4} }] should return this: { {{1, 3}, {2, 4}}, {{2, 2}, {3, 3}}, {{3, 1}, {4, 2}} } It should be fast enough to work quickly on 3000x2000 matrices with 100x100 sub-matrices. For simplicity, it is enough to only consider integer matrices

Mathematica - Separating Notebooks [closed]

半世苍凉 提交于 2019-11-29 19:38:55
Is there a way to separate open Mathematica notebooks so that they don't share any variables? How about making it so some variables are shared but not all? Yes, there is. I recommend reading documentation related to Mathematica contexts. In a nutshell, all variables belong to some context (namespace), and all variables can be accessed via their fully-qualified names of the form "ContextName`varName". If you just use "varName", Mathematica will search contexts in $ContextPath (try evaluating the variable $ContextPath to see what it is), and will use the first context where it finds that