wolfram-mathematica

How should I write a function to be used in Apply in Mathematica?

你说的曾经没有我的故事 提交于 2019-12-04 12:11:30
I am wondering how I can write a function to be used in the Apply function in Mathematica? For example, I want to trivially re-implement the Or function, I found the following Apply[(#1 || #2)&,{a,b,c}] is not okay since it only Or 'ed the first two elements in the list. Many thanks! This will work, no matter how many vars, and is a general pattern: Or[##]&, for example In[5]:= Or[##] & @@ {a, b, c} Out[5]= a || b || c However, in the case of Or , this is not good enough, since Or is HoldAll and short-circuiting - that is, it stops upon first True statement, and keeps the rest unevaluated.

Using Fold to calculate the result of linear recurrence relying on multiple previous values

こ雲淡風輕ζ 提交于 2019-12-04 11:55:31
I have a linear recurrence problem where the next element relies on more than just the prior value, e.g. the Fibonacci sequence. One method calculating the n th element is to define it via a function call, e.g. Fibonacci[0] = 0; Fibonacci[1] = 1; Fibonacci[n_Integer?Positive] := Fibonacci[n] + Fibonacci[n - 1] and for the sequence I'm working with, that is exactly what I do. (The definition is inside of a Module so I don't pollute Global` .) However, I am going to be using this with 2 10 - 2 13 points, so I'm concerned about the extra overhead when I just need the last term and none of the

Moving contexts with $NewSymbol in Mathematica

梦想的初衷 提交于 2019-12-04 11:50:42
问题 I was playing around with $NewSymbol trying to find something to answer this question with. The docs say that $NewSymbol is applied before the symbol is actually created. If the action of $NewSymbol causes the symbol to be created, perhaps in a different context, then the symbol as created will be the one used. So I tried to automatically move a particular new symbol into a test context which should prevent its creation in the Global`* context, but the symbol gets created in both contexts. In

A variation of IntegerPartition?

限于喜欢 提交于 2019-12-04 11:24:10
问题 IntegerPartitions[n, {3, 10}, Prime ~Array~ 10] In Mathematica this will give a list of all the ways to get n as the sum of from three to ten of the first ten prime numbers, allowing duplicates as needed. How can I efficiently find the sums that equal n, allowing each element to only be used once ? Using the first ten primes is only a toy example. I seek a solution that is valid for arbitrary arguments. In actual cases, generating all possible sums, even using polynomial coefficients, takes

TransformedDistribution in Mathematica

谁都会走 提交于 2019-12-04 11:18:41
I have developed some code to generate random variates from the product of a LogNormalDistribution and a StableDistribution: LNStableRV[{\[Alpha]_, \[Beta]_, \[Gamma]_, \[Sigma]_, \[Delta]_}, n_] := Module[{LNRV, SDRV, LNSRV}, LNRV = RandomVariate[LogNormalDistribution[Log[\[Gamma]], \[Sigma]], n]; SDRV = RandomVariate[ StableDistribution[\[Alpha], \[Beta], \[Gamma], \[Sigma]], n]; LNRV * SDRV + \[Delta] ] (* Note the delta serves as a location parameter *) I think this works fine: LNStableRV[{1.5, 1, 1, 0.5, 1}, 50000]; Histogram[%, Automatic, "ProbabilityDensity", PlotRange -> {{-4, 6}, All}

output with “Private`” Content in Mathematica Package

我们两清 提交于 2019-12-04 11:04:20
问题 I am trying to solve the following implementation problem in Mathematica 7.0 for some days now and I do not understand exactly what is happening so I hope someone can give me some hints. I have 3 functions that I implemented in Mathematica in a source file with extension *.nb. They are working okay to all the examples. Now I want to put these functions into 3 different packages. So I created three different packages with extension .*m in which I put all the desired Mathematica function. An

Efficient alternative to Outer on sparse arrays in Mathematica?

空扰寡人 提交于 2019-12-04 10:53:01
问题 Suppose I have two very large lists {a1, a2, …} and {b1, b2, …} where all ai and bj are large sparse arrays. For the sake of memory efficiency I store each list as one comprehensive sparse array. Now I would like to compute some function f on all possible pairs of ai and bj where each result f[ai, bj] is a sparse array again. All these sparse arrays have the same dimensions, by the way. While Flatten[Outer[f, {a1, a2, ...}, {b1, b2, ...}, 1], 1] returns the desired result (in principle) it

How to Convert an Alphanumeric (Reference) Number Containing a Decimal Point to a String in Mathematica

本秂侑毒 提交于 2019-12-04 10:30:28
I have a reference number of the following type DAA76647.1 which I want to convert unchanged to a string in Mathematica. That is myfn[DAA76647.1] gives as output "DAA76647.1" Is there an easy way to do this? (The input cannot be a string and, other than conversion to a string, I do not want to change the input in any other way). Update ToString /@ {A1234, 1234.1, A1234 .5} gives the following output (where I have simply entered everything from the keyboard) {"A1234", "1234.1", "0.5 A1234"} It appears that if what goes before the decimal point is alphanumeric, there is a problem. Possible

What is causing this strange Mathematica result?

元气小坏坏 提交于 2019-12-04 10:07:36
I've run into what seems like a bug in Mathematica 8. I can't find anything related to it online, but I admit I'm not exactly sure what to search for. If I run this statement: 0.05 + .10 /. {0.15 -> "pass"} 1.04 + .10 /. {1.14 -> "pass"} 1.05 + .10 /. {1.15 -> "pass"} 1.15 /. {1.15 -> "pass"} I get this output: pass pass 1.15 pass Am I just overlooking something? Edit: After reading the helpful discussion below, I changed my dispatch table to use a Which statement instead: f[x_] := Which[x == 1.05, -1.709847, x == 1.10, -1.373823, x == 1.15, -1.119214, x == 1.20, -0.9160143, x == 1.25, -0

Getting coordinates of manually drawn points

Deadly 提交于 2019-12-04 10:05:39
I have a graph as a result of executing ListPlot[] function. I can manually edit this graph by moving points to a different location and also adding new points using the Drawing Tools . How do I get the coordinates of new and changed points from the edited graphics? This approach makes every data point a locator that can be moved. New locators can be added and old ones deleted as appropriate. The best fit and variance are updated after every change. Here's some data of some exponential growth with some errors and a data point missing data = Delete[Table[{t, (1 + RandomReal[{-.2, .2}])Exp[t]},