Tag Info

New answers tagged

1

Having upgraded to Mathematica 10.0.0, it turns out that the Distribution library greatly facilitates the numerical simulation of boson-sampling experiments using the above PermanentCode` package. The built-in function KolgomorovSmirnovTest is particularly valuable; the Wikipedia entry Kolmogorov–Smirnov test provides a good introduction. The appended ...


3

As far as I understand the concept there is no true pass-by-reference in Mathematica. Attempting to make assignments to arguments is in fact a common mistake which I addressed in this answer: Attempting to make an assignment to the argument of a function. As described there one needs a Hold attribute for in-place modification of definitions, e.g.: ...


5

How about using "RuntimeErrorHandler": f = Compile[{{x, _Real}, {y, _Real}}, Log[(x - y^2 - 2. x)^2]/(y x^2 - 2 (x + y) - y^2 + 3.), "RuntimeOptions" -> {"RuntimeErrorHandler" -> Function[Throw[$Failed]]} ]; Catch[Quiet@f[-196, 15]] // AbsoluteTiming (* {0.000019, 0.0000116843} *) Catch[Quiet@f[-196, 14]] // AbsoluteTiming (* {0.000051, ...


4

I use \$MessagePrePrint = StandardForm since without that the real number 1.5 is displayed in a message as 1.5`. However, you might have $MessagePrePrint set to something else. Check is used to control what should happen when a built-in message occurs. Quiet prevents the built-in message from being displayed. I made a pure function (i.e. #1,#2,& ...


3

If you use an intermediate function as demonstrated in this answer, you'll get rid of all error messages that are related to the symbolic evaluation: derivativeStrategyLongStraddleCompiled2[currentPrice_?NumberQ, strikePrice_?NumberQ, callPremium_?NumberQ, putPremium_?NumberQ] := derivativeStrategyLongStraddleCompiled[currentPrice, strikePrice, ...


4

No, Mathematica does not include a C compiler. So if you want to learn C you will need to install a C compiler.


1

Span can be compiled: << CompiledFunctionTools` cf = Compile[{{lst, _Real, 1}}, lst[[;; 2]]]; CompilePrint@cf Pictured by Simon Wood's shadow. All used as an argument of Part can be compiled: cf2 = Compile[{{lst, _Real, 2}}, lst[[All, ;; 2]]]; CompilePrint@cf2 while All used as an argument of Span, which is the FullForm of (* a number* ) ...


2

The following are compilable in M10 but aren't in previous versions: {Indexed, LogisticSigmoid}


11

Okay, this is a bit of an embarassment. Here is a very small modification of the original code. I simply made explicit option settings, made a denominator to Sin explicitly real, that kind of thing. My tests show the same timing as the original, give or take an iota. ie = 200; ez = ConstantArray[0., {ie + 1}]; hy = ConstantArray[0., {ie}]; fdtd1d = ...


6

Introduction The update of the question invalidates my original approach, which depended on the problem being a simple special case. The current problem consists of a linear objective function with linear constraints, to be maximized over the integers. Theoretically, one can solve the problem with LinearProgramming. In the OP's setup, the variables betha ...


3

Edited answer If I reinterpret your constraints so they make sense, constraints = Apply[And, Flatten@{ Apply[LessEqual, {0, #, 1}] & /@ Flatten@subcindexSet, Map[Total@# == 1 &, Transpose@subcindexSet, {1}] }] (* 0 <= betha11 <= 1 && 0 <= betha12 <= 1 && 0 <= betha13 <= 1 ...


3

So we have a couple things going on here. First, as mentioned in an answer to your previous question, ExpIntegralEi is not compilable (MainEvaluate called). So speedup from compiling this function will be negligible and it may be easier to not even compile in this case. Second, to properly use NMaximize we need the function to accept numeric arguments ...


4

Since gh is a tensor, you need to say what rank it is, so replace {gh, _Real} with {gh, _Real, 2} to fix the error. costFxn = Compile[ {{P, _Real}, {Ns, _Integer}, {gh, _Real, 2}, {Kg, _Integer}, {G, _Integer}, {betaGN, _Integer}}, Sum[ -Exp[Kg/(P gh[[g, n]])] (Kg * betaGN)/ Log[2] ...


0

The correct code is: lyapexp[n_] := ReleaseHold[Replace[Hold[Compile[{{kMin, _Integer}, tol, r, u0, u1}, Module[{ relErr, ohs, res = {0., 0.}, hs = Table[.0, {n}], k = 0, nzs = Table[.0, {n}], ss = Table[.0, {n}], ws = {{1., 0.}, {0., 1.}}, zs, success = 1.0, uPrev = u0, ...


4

I suggest that you use local rules for your fixed values. Then, the following seems to do what you ask for: ClearAll[dotQ, init, vars]; dotQ[s_String, MSymbol_Symbol] := StringMatchQ[s, ToString[MSymbol] ~~ LetterCharacter]; vars[MSymbol_] := Select[Not@*(dotQ[#, MSymbol] &)@*ToString]@*Variables; init[expr_, MSymbol_Symbol] := Thread[#, Hold] ...


8

Let me elaborate my comment into an answer. To make the nested Sum compiled, let's first have a close look at the compiling result of code containing one Sum: sum = Compile[{{n, _Integer}}, 1/Sum[3.141 + j, {j, n}]]; Needs["CompiledFunctionTools`"] p1 = CompilePrint@sum It's not hard to notice that Sum is actually translated into a loop by Compile. It's ...


7

Following the 10.9.5 patch to MacOS and the corresponding update to XCODE it may be necessary to run XCODE again to reinstall it and then this should renable C compilation in Mathematica (v10) and SystemModeler (v3). Certainly was the case for me.



Top 50 recent answers are included