wolfram-mathematica

how to automatically load user-defined functions in mathematica

末鹿安然 提交于 2019-12-20 12:41:13
问题 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! 回答1: You can create a package in $UserBaseDirectory/Autoload. This will be loaded at Kernel initialization time. Your package should have a Kernel/init.m

Unwanted evaluation in assignments in Mathematica: why it happens and how to debug it during the package-loading?

梦想的初衷 提交于 2019-12-20 10:57:57
问题 I am developing a (large) package which does not load properly anymore. This happened after I changed a single line of code. When I attempt to load the package (with Needs), the package starts loading and then one of the setdelayed definitions “comes alive” (ie. Is somehow evaluated), gets trapped in an error trapping routine loaded a few lines before and the package loading aborts. The error trapping routine with abort is doing its job, except that it should not have been called in the first

Faster huge data-import than Get[“raggedmatrix.mx”]?

笑着哭i 提交于 2019-12-20 10:57:34
问题 Can anybody advise an alternative to importing a couple of GByte of numeric data (in .mx form) from a list of 60 .mx files, each about 650 MByte? The - too large to post here - research-problem involved simple statistical operations with double as much GB of data (around 34) than RAM available (16). To handle the data size problem I just split things up and used a Get / Clear strategy to do the math. It does work, but calling Get["bigfile.mx"] takes quite some time, so I was wondering if it

Mathematica Downvalue Lhs

霸气de小男生 提交于 2019-12-20 10:39:54
问题 Does anybody know if there is a built-in function in Mathematica for getting the lhs of downvalue rules (without any holding)? I know how to write the code to do it, but it seems basic enough for a built-in For example: a[1]=2; a[2]=3; BuiltInIDoNotKnowOf[a] returns {1,2} 回答1: This seems to work; not sure how useful it is, though: a[1] = 2 a[2] = 3 a[3] = 5 a[6] = 8 Part[DownValues[a], All, 1, 1, 1] 回答2: This is like keys() in Perl and Python and other languages that have built in support for

Custom ColorFunction/ColorData in ArrayPlot (and similar functions)

爱⌒轻易说出口 提交于 2019-12-20 09:59:46
问题 This is related to Simon's question on changing default ColorData in Mathematica. While the solutions all addressed the issue of changing ColorData in line plots, I didn't quite find the discussion helpful in changing the ColorFunction / ColorData in ContourPlot / ArrayPlot / Plot3D , etc. TLDR: Is there a way to get mma to use custom colors in ArrayPlot/ContourPlot/etc. Consider the following example plot of the function sin(x^2+y^3) that I created in MATLAB: Now doing the same in mma as:

How to find a function's rth derivative when r is symbolic in Mathematica?

可紊 提交于 2019-12-20 09:56:00
问题 I have a function f(t)=2/(2-t) . It is not so hard to get the rth derivative at t=0 (i.e. 2^(-r)*r! ) without using Mathematica. In the case of Mathematica calculation, I can get the r-th derivative when r=4 like this: D[2/(2-t), {t, 4}] . But how can I get the rth derivative at t=0 in Mathematica when r is ANY integer? I tried to use this expression, but it didn't work as expected: Simplify[D[2/(2 - t), {t, r}], Assumptions -> Element[r, Integers]] /. {t->0} Is it possible to do the above

Is Mathematica an untyped language?

点点圈 提交于 2019-12-20 09:47:11
问题 Unlike most programming languages, every value in Mathematica is an expression. Applying any operation to any expressions always yields another expression. Consequently, Mathematica effectively has only one type. Mathematica does no static type checking and, arguably, doesn't even check types dynamically (at run-time). For example, adding the integer expression 1 to the string expression "foo" in Mathematica results in the (nonsensical) expression 1 + "foo" but no error. In other cases,

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

孤者浪人 提交于 2019-12-20 08:47:27
问题 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

What does “upvalue” mean in Mathematica and when to use them?

一个人想着一个人 提交于 2019-12-20 08:44:37
问题 To me, g /: f[g[x_]] := h[x] is just verbose equivalent of f[g[x_]] := h[x] . Can you raise an example that you have to use /: ? 回答1: Actually, g /: f[g[x_]] := h[x] is not equivalent to f[g[x_]] := h[x] . The latter associates the definition with f , while TagSet ( /: ) and UpSet ( ^= and its delayed version, ^:= ) associate the definition with g . This is a crucial difference and can be illustrated by a simple example. Let's say you want to have a set of variables that obey modulo 5

What does “upvalue” mean in Mathematica and when to use them?

别说谁变了你拦得住时间么 提交于 2019-12-20 08:42:52
问题 To me, g /: f[g[x_]] := h[x] is just verbose equivalent of f[g[x_]] := h[x] . Can you raise an example that you have to use /: ? 回答1: Actually, g /: f[g[x_]] := h[x] is not equivalent to f[g[x_]] := h[x] . The latter associates the definition with f , while TagSet ( /: ) and UpSet ( ^= and its delayed version, ^:= ) associate the definition with g . This is a crucial difference and can be illustrated by a simple example. Let's say you want to have a set of variables that obey modulo 5