wolfram-mathematica

Sprintf equivalent in Mathematica?

孤街浪徒 提交于 2019-12-03 01:15:46
I don't know why Wikipedia lists Mathematica as a programming language with printf . I just couldn't find the equivalent in Mathematica. My specific task is to process a list of data files with padded numbers, which I used to do it in bash with fn=$(printf "filename_%05d" $n) The closest function I found in Mathematica is PaddedForm . And after some trial and error, I got it with "filename_" <> PaddedForm[ Round@#, 4, NumberPadding -> {"0", ""} ]& It is very odd that I have to use the number 4 to get the result similar to what I get from "%05d". I don't understand this behavior at all. Can

Optional named arguments in Mathematica

允我心安 提交于 2019-12-03 00:48:42
What's the best/canonical way to define a function with optional named arguments? To make it concrete, let's create a function foo with named arguments a , b , and c , which default to 1, 2, and 3, respectively. For comparison, here's a version of foo with positional arguments: foo[a_:1, b_:2, c_:3] := bar[a,b,c] Here is sample input and output for the named-arguments version of foo : foo[] --> bar[1,2,3] foo[b->7] --> bar[1,7,3] foo[a->6, b->7, c->8] --> bar[6,7,8] It should of course also be easy to have positional arguments before the named arguments. dreeves I found the standard way to do

Why would Mathematica break normal scoping rules in Module?

我是研究僧i 提交于 2019-12-03 00:28:10
As was pointed out in a recent post scoping does not work as expected inside of Module. An example from that thread is: Module[{expr}, expr = 2 z; f[z_] = expr; f[7]] (*2 z*) But the following works almost as expected. Module[{expr}, expr = 2 z; Set@@{f[z_], expr}; f[7]] (*14*) What language design consideration made wolfram choose this functionality? Edit: See Jefromi's first comment I changed z from being a local variable to not and forgot to change the output. It does not effect the problem. Edit2: Michael Pilat's point seems to be that Block and Module have different functions. I think I

Import from web - set user agent in Mathematica

社会主义新天地 提交于 2019-12-03 00:25:41
when I connect to my site with Mathermatica ( Import["mysite","Data"] ) and look at my Apache log I see: 99.XXX.XXX.XXX - - [22/May/2011:19:36:28 +0200] "GET / HTTP/1.1" 200 6268 "-" "Mathematica/8.0.1.0.0 PM/1.3.1" Could I set it to be something like this (when I connects with real browser): 99.XXX.XXX.XXX - - [22/May/2011:19:46:17 +0200] "GET /favicon.ico HTTP/1.1" 404 183 "-" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24" Mathematica 9 has the new URLFetch function. It has the option UserAgent. As far as I know you can't change the

Is there a fast product operation for PackedArrays?

跟風遠走 提交于 2019-12-03 00:18:22
In Mathematica a vector (or rectangular array) containing all machine size integers or floats may be stored in a packed array. These objects take less memory, and some operations are much faster on them. RandomReal produces a packed array when possible. A packed array can be unpacked with the Developer function FromPackedArray Consider these timings lst = RandomReal[1, 5000000]; Total[lst] // Timing Plus @@ lst // Timing lst = Developer`FromPackedArray[lst]; Total[lst] // Timing Plus @@ lst // Timing Out[1]= {0.016, 2.50056*10^6} Out[2]= {0.859, 2.50056*10^6} Out[3]= {0.625, 2.50056*10^6} Out

Extracting an expression matching a pattern from a large expression

此生再无相见时 提交于 2019-12-03 00:12:31
I have a Mathematica expression that contains a single square root, schematically expr = a / (b + Sqrt[c]); where a , b , c are large expressions. I would like to extract the expression under the sqrt, for instance by matching to a pattern, something like Match[expr,Sqrt[x_]] // should return c Is there an easy way to do this? Theoretically, this should work correctly: extractSqrt = Cases[ToBoxes@#, SqrtBox@x_ :> ToExpression@x, Infinity] &; extractSqrt[expr] If you are willing to change the assignment to expr , you can do this: expr = Hold[a / (b + Sqrt[c])]; Cases[expr, HoldPattern @ Sqrt[x_

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

匆匆过客 提交于 2019-12-02 23:57:51
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 would be quicker to use BLOBs or whatever with PostgreSQL or MySQL or whatever database people use for

Question on Condition (/;)

我的未来我决定 提交于 2019-12-02 23:52:44
Condition has attribute HoldAll which prevents evaluation of its first argument before applying the Condition . But for some reason Condition evaluates its first argument even if the test gives False : In[1]:= Condition[Print[x],False] During evaluation of In[1]:= x Out[1]= Null/;False Why is this? For what purposes Condition evaluates its first argument if the test gives False ? In which cases this behavior can be useful? P.S. Its behavior differs when the Condition is used as the second argument of SetDelayed : In[5]:= f:=Condition[Print[x],False]; f Out[6]= f This is what I expected for the

Mathematica Downvalue Lhs

隐身守侯 提交于 2019-12-02 23:52:35
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} 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] dreeves This is like keys() in Perl and Python and other languages that have built in support for hashes (aka dictionaries). As your example illustrates, Mathematica supports hashes without any special

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

别等时光非礼了梦想. 提交于 2019-12-02 23:49:00
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 place, during the package loading phase. The error message reveals that the wrong argument is in fact