Tag Info

Hot answers tagged

16

Unless both lists given to Equal are packed arrays Equal will first unpack. Unfortunately for this case {} is not a packable expression, therefore list == {} will always unpack list, assuming it starts packed. That unpacking takes time: test = RandomInteger[100000000, 10000000]; Developer`FromPackedArray[test]; // AbsoluteTiming {0.207012, Null} ...


12

Maybe upperTriangularMatrixQ2[mat_?MatrixQ] /; Equal @@ Dimensions@mat := UpperTriangularize@mat == mat; test = RandomInteger[{1, 100}, {1000, 1000}]; upperTriangularMatrixQ@test // AbsoluteTiming {2.126050, False} upperTriangularMatrixQ2@test // AbsoluteTiming {0.003277, False} test2 = UpperTriangularize@test; upperTriangularMatrixQ@test2 ...


9

You did not specify if this test should be optimized for the positive or negative case. If most of your matrices will fail the test it can be greatly beneficial to have an early exit behavior. For example if the lower left element in the matrix is not zero you can fail the matrix after a single element test! And even in the positive case the elements on ...


8

Every expression in Mathematica is characterized by it's FullForm. So in your case particularly FullForm[Log[x*x]]===Log[Power[x,2]] and FullForm[2*Log[x]]===Times[2,Log[x]] These two, although equivalent from a Mathematical point of view (but only if x>0) they have different FullForm representations. So SameQ(===) checks if the expression trees are ...


7

Finally: test5 = OrderedQ @ Reverse @ Unitize @ # & slow stuff: test = MatchQ[#, {Except[0] .., (0) ...}] & new one, this is "only" two orders of magnitude slower than Mr.Wizard's :) test3 = Length[Split[#, Count[{##}, 0] != 1 &]] <= 2 & getting closer, only twice as long: test4 = Length @ Split @ Unitize @ # <= 2 &


6

The first pattern that came to mind: p1 = {___, 0, Except[0], ___}; ! MatchQ[{2, 3, 17}, p1] ! MatchQ[{2, 3, 17, 0, 0}, p1] ! MatchQ[{1, 0, 1, 0, 1}, p1] True True False I am exploring other avenues now. It seems that this pattern is vastly more efficient that Kuba's superficially similar one. As a simple example: pK = {Except[0] .., (0) ...}; ...


5

For big collections of lists, this should be quick: fx = With[{s = SparseArray[PadRight@#]["AdjacencyLists"]}, SameQ @@@ Transpose[{Length /@ s, Last /@ Replace[s, {} -> {0}, 1]}]] &; Update: Even more so: fx2 = OrderedQ /@ Unitize[#[[All, -1 ;; 1 ;; -1]]] &; Compare (old netbook timings... seems to clobber other answers so far...): (* ...


4

You need to construct your desired expression without unwanted evaluation of its constituent parts. Here is one approach to do that: makeTest[{tests__}] := Replace[And[tests] &, fn_ :> fn[#], {2}] This works because the surrounding Function prevents evaluation before and after the Replace operation. Example: f1[x_] := (Print["First"]; x > ...


3

In your code p_ simply means a pattern (Blank[]) that you want to name p. Normally this (giving it a name) is done because you want to do something with the matched pattern. The point here is that you are not testing for a subscripted variable that starts with p. You code is essentially equivalent to MemberQ[pars2, Subscript[\[Tau], _]] which is why you get ...


3

I don't know if I understand well, but if you want to check, with Mathematica on Unix, if a given file/directory is executable, you'll have to run a specific external unix command line and retrieve the result. Unix useful commands The following concerns UNIX systems but there are probably some equivalent commands for Windows. (Feel free to edit the post if ...


3

I just checked the documentation in V10 and accidentally stumbled upon a built in command: SQLConnectionOpenQ[conn] This seems to do the trick.


3

My apologies to those who closed this question for my unilaterally reopening it, but there is a nontrivial aspect to this question that I wish to address, and it would not nicely fit in comments. (I am not making an exception for myself; when someone has such an answer he wishs to give to a closed question I nearly always reopen it for him to do so.) ...


2

Let's see if we can simplify the decision task a bit. Let $$f = b (p-1) x^{-p}-\gamma +\eta _1 k_0+\frac{\beta _0 \left(\eta _1+\eta _2-1\right) (n-1) x^n}{\left(x^n+1\right)^2}+\frac{\beta _0 \left(\eta _1+\eta _2-1\right)}{\left(x^n+1\right)^2}$$ With the abbreviations $$r = b (p-1), s = \beta _0 \left(\eta _1+\eta _2-1\right), t = -\gamma +\eta _1 ...


2

Primary fixes: Adding First to fidelPhase and adding t to Subscript[u, i, j][t]. Making an objective function obj that is not evaluated until a and b are numeric may or may not be important. I won't have time to check it out (I've lost track of the original optimizer). The following works and it's not crucial to be so precise in one's fixes. ...


2

Here are two bulky yet fast compiled functions. The two functions are essentially the same, but the second one is slightly adapted to rashers test case for timing comparisons. In my previous version they were even longer, but it turns out they are faster this way. cfu = Compile[ {{ints, _Integer, 1}} , Block[ {len, zFlag, res} , res = True; ...


2

ClearAll[f1,f2] f1 = With[{u = Unitize@#}, FreeQ[u[[;; Tr@u]], 0]] &; f1 /@ {{1, 2, 3}, {0, 1, 2, 3, 0}, {1, 2, 3, 0, 0, 0}, {0, 0, 1, 2, 3}} (* {True, False, True, False} *) f2 = With[{u = Unitize@#}, Times @@ N @ u[[;; Tr@u]] != 0] &; f2 /@ {{1, 2, 3}, {0, 1, 2, 3, 0}, {1, 2, 3, 0, 0, 0}, {0, 0, 1, 2, 3}} (* {True, False, True, False} *)


1

Using Complement on two lists could be used as follows: Complement[l1, l2] == {} True If you have more than one list, for example, l1 = {a, b, c}; l2 = {b, c, a}; l3 = {c, b, z}; you could also implement it with Tuples and compare the lists pairwise: ((Complement @@ #) == {}) & /@ Tuples[{l1, l2, l3}, 2] {True, True, False, True, True, ...


1

Daniel Lichtblau confirmed that this was a bug. It has been corrected in version 10.1.0.


1

f[x_] := Plus @@ (Join[x, {0, 0}] /. {___, 0, r__} :> {r}) == 0 f /@ {{0, 1, 2, 3, 0}, {1, 2, 3, 0, 0, 0}, {0, 0, 1, 2, 3}} (* {False, True, False} *) SeedRandom[0] x = RandomInteger[999, 20000]; f@x // Timing // First (* 0. *)



Only top voted, non community-wiki answers of a minimum length are eligible