Tag Info

Hot answers tagged

29

I'm coming to the party a bit late, but here's my approach. It should work for any two polygons, including non-convex and self-intersecting ones. winding[poly_, pt_] := Round[(Total@ Mod[(# - RotateRight[#]) &@(ArcTan @@ (pt - #) & /@ poly), 2 Pi, -Pi]/2/Pi)] cross[e1_, e2_] /; (N[Det[{Subtract @@ e1, Subtract @@ e2}]] === 0.) = ...


29

Both, And and Or should work for All and Any respectively. You may have to get creative in how you apply them, though. For instance, And @@ {True, False, True} works just like you would expect AllOf @ {True, False, True} to without any additional work. Similarly, Or @@ {False, True, False} works like AnyOf.


23

You can implement equivalents of the any and all functions in MATLAB and python in Mathematica using the MemberQ and FreeQ functions as: any[x_List] := MemberQ[x, True] all[x_List] := FreeQ[x, False] For large lists, these will be about an order of magnitude faster in the worst case to several orders faster in the best case, when compared to the And and ...


18

I prefer using Inner for this, as conceptually, it is a generalized dot of the two Boolean lists with And as the operator. Inner[And, {True, True, False}, {True, False, False}, List] (* {True, False, False} *)


16

How about RegionPlot? RegionPlot[ { (x - 0.2)^2 + y^2 < 0.5 && 0 < x < 1 && 0 < y < 1, (x - 0.2)^2 + y^2 < 0.5 && ! (0 < x < 1 && 0 < y < 1), ! ((x - 0.2)^2 + y^2 < 0.5) && 0 < x < 1 && 0 < y < 1 }, {x, -1, 1.5}, {y, -1, 1.5}, PlotStyle -> ...


16

Have you seen, that Mathematica is capable of many boolean computations using special boolean functions? Let's assume someone from the island makes a statement, then when the statment is true, whether or not he tells the statement is true, depends on whether or not he is a truth-teller. When we know, which kind he is, we know the correct statement through ...


15

I like more : MapThread[ And, {{True, True, False}, {True, False, False}}] {True, False, False} Edit We should test efficiency of various methods for a few different lists. Definitions Argento[l_] := (And @@ # & /@ Transpose[l]; // AbsoluteTiming // First) Brett[l_] := (And @@@ Transpose[l]; // AbsoluteTiming // First) Artes[l_] := ...


14

In addition to the simple form where you already have a list of True|False elements, you may want lazy evaluation in creating that list, short circuiting if the test fails. You can do this with Hold. I include a Print statement so that you can see what actually evaluates: (Print@#; # != 0) & /@ Hold[1, 0, 0, 1, 1, 0, 1, 0] And @@ % (Print@#; # != ...


14

I like Artes' and kguler's answers, but I'd like to point out that in general f @@ # & /@ list can be more concisely written as f @@@ list For example: And @@@ Transpose[{{True, True, False}, {True, False, False}}] (* {True, False, False} *)


13

The (undocumented!) function Graphics`Mesh`PolygonIntersection[] seems up to the task. Using Sjoerd's example: Graphics`Mesh`MeshInit[]; polys = {Polygon[{{1, 3}, {3, 4}, {4, 7}, {5, -1}, {3, -3}}], Polygon[{{2, 2}, {3, 3}, {4, 2}, {0, 0}}]}; Graphics[Append[{Gray, polys}, {Blue, PolygonIntersection[polys]}]] Disk[] objects are not covered by ...


13

I am not aware of any built-in functionality (I might easily be wrong), but there's an example at MathWorld for calculating intersections of convex polygons. You'd need to approximate the circle with a polygon. Get the notebook from that page: there's an intersection calculation inside that uses the IMTEK Mathematica supplement. Example: << ...


13

You want to complement bits based on a given length. Easy enough. complementBits[bits_Integer, len_Integer] /; bits >= 0 && len >= 0 := BitXor[bits, 2^len - 1] (If you really want to compliment them, tell them the size is much too big for them..) Quick test. complementBits[43, 8] (* Out[237]= 212 *) Getting back to the question at ...


12

You can use And and still get what you want: x := 1 If[And @@ {True, False, True, False, (x := 2; True)}, Print["Yes"], Print["No"]]; x (* No 2 *) What happens is that the List and its arguments are evaluated before And is applied to it, hence setting the value of x.


11

You can use Thread: Thread[And[{True, True, False}, {True, False, False}]] or, Thread with Apply (@@) : Thread[And@@{{True, True, False}, {True, False, False}}] (* {True, False, False} *)


11

Unfortunately, the approach of using Apply[Plus, Flatten[nbhd]] is fatal to your algorithm because summing up the cell-values introduces ambiguities. As an example take these rules from your excel file (White (0), Red (1) and Black (2)). This is in the Excel-file at A43: {{2,2,2},{2,2,0},{0,0,0}} -> 2 And this is in A179: {{1,1,1},{1,2,1},{1,1,1}} ...


9

Another way for All is to use VectorQ function VectorQ[lis, TrueQ]


9

v1 = RandomChoice[{-1, 1}, 100]; v2 = RandomChoice[{-1, 1}, 100]; vL = UnitStep[v1]; vL*v1 + (1 - vL)*v2 Using PatoCriollo's comparison (and now updated to include rasher and Mr. Wizard's methods): n = 10^6; v1 = RandomChoice[{-1, 1}, n]; v2 = RandomChoice[{-1, 1}, n]; (bp = Boole[Positive[v1]]; test1 = bp v1 + (1 - bp) v2); // Timing test2 = With[{c = ...


8

Another option is to use image processing features such as ImageCompose: { g1 = Graphics[Rectangle[], PlotRange -> 1], g2 = Graphics[Disk[{0.2, 0}, .5], PlotRange -> 1], ImageCompose[g1, g2, Center, Center, {1, 1, 0}] } The output of the above looks like this: (Note that in this case your Graphics get rasterized and the result is an Image.)


8

As in the comments above, we see that the HoldAll attribute is causing the "problem". Note that if we unset HoldAll it works: Unprotect@And; ClearAttributes[And, HoldAll]; Protect@And; Then: And[check] True EDIT: based on Oleksandr R.'s comment, I must stress that I showed this only to illustrate the "problem". It is not a good idea to unset ...


8

In addition to HoldAll as highlighted by Pinguin Dirk the other component of the behavior is that And directly returns single arguments: And[73] 73 Combined, And[check] spits out check which at the top level evaluates to Sequence[True, . . .]. One problem with your method for checking the matrix is that it does not short-circuit on a non-numeric ...


8

The Wolfram Language and Mathematica 10 (available now on the Raspberry Pi) have new functions — AnyTrue, AllTrue, NoneTrue — which take a predicate and test any/all/none on the input list. For example: AnyTrue[Range@5, EvenQ] (* True *) AllTrue[{True, False, False}, TrueQ] (* or Identity in place of TrueQ *) (* False *) These functions can also be ...


8

BooleanMinimize[BooleanFunction[table]] (* (#1 && ! #2) || (#1 && ! #3) || (#1 && ! #4) || (! #1 && #2 && #3 && #4) & *) Note in the documentation how 1/0 vs True/False are treated here, massage as needed. Quick verification: bf = BooleanFunction[table]; Rule @@@ Transpose[{table[[All, 1]], ...


7

tutorial/RealPolynomialSystems claims "Reduce, Resolve, and FindInstance always put real polynomial systems in the prenex normal form, with quantifier-free parts in the disjunctive normal form..." For obtaining Skolem form from prenex, possibly could proceed as described at http://demonstrations.wolfram.com/Skolemization/ or ...


7

You may be able to do this using FilledCurve in version 8. I see examples of subtraction and exclusion but not intersection.


7

Given the rule as explained by Halirutan... If the number of Red cells is equal to the number of Black cells, then the center cell gets White. Otherwise the center gets the color Red, if we have more Red cells and Black if we have more Black cells. ...it's interesting to note how things work if we use the following mapping of colours to numbers: ...


6

f[{a_, b_, c_, d_}] := BooleanFunction[Thread[Tuples[{0, 1}, 2] -> {a, b, c, d}]] Usage f[{True, False, True, False}][0, 1] (* False *)


6

You can do this by explicitly constructing the InterpolatingPolynomial corresponding to yan, and then using FullSimplify: yin = InterpolatingPolynomial[Transpose[Flatten /@ {yan[[3]],yan[[4]]}],x]; FullSimplify[yin > -1, -1 < x < 1] (*True*) Why does this work? Because yan actually has a list of points: FullForm[yan] so I can extract them ...


6

This happens because Simplify converts the expression to a BooleanFunction representation. Checking the documentation for BooleanFunction you find that: Elements of both inputs and outputs can be specified either as True and False or as 1 and 0.


6

Just to illustrate what the comments are getting at, consider these variations below. First, the three If calculations are consistently faster than the corresponding operator versions. Second, the time it takes to execute Print is highly variable (see below). Finally, let me add that operators are limited in their possible values (for example, Or yields ...


6

Here is a method using SparseArray properties to find positions (indexes), then Part to make the replacements. It may be a bit more general than the mathematical methods and it appears to be roughly as efficient on my system (running version 7). Note that unless it is acceptable to count zero as positive it is inappropriate to use UnitStep[v1] -- instead ...



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