Tag Info

Hot answers tagged

9

How about this? nextpow2[a_] := Ceiling@Log[2, Abs@a]; The same thing in a different style: f1 = Ceiling @* Log2 @* Abs; (* v10 syntax *) Or: f2 = ⌈Log2 @ Abs @ #⌉ &; A plot: Plot[f2[x], {x, -10, 10}, Filling -> 0]


7

As Kuba comments Level follows the standard expression traversal order: Level traverses expressions in depth-first order, so that the subexpressions in the final list are ordered lexicographically by their indices. This is actually a depth-first postorder traversal. It is the normal order in which expressions are evaluated in Mathematica: echo[x_] := ...


5

You can use the new (in V10) ImplicitRegion function as follows: reg = ImplicitRegion[0 <= x <= 1, {x}]; Then: ArgMax[f1[x], x ∈ reg]


4

I think you need a clear differentiation of "constant" and "variable" and I don't see that in your question. For the sake of the example let us consider anything numeric to be a constant and anything else to be a variable, so my PatternTest function will be NumericQ. func[f_, a_?NumericQ g_] := a func[f, g] Now: func[foo, 6 bar] func[x, Pi y^2] 6 ...


3

An alternative workaround is to convert the BoundaryMeshRegion into a MeshRegion from the MeshCoordinates and MeshCells. This lets you use HighlightMesh as desired: SeedRandom[0]; pts = RandomReal[4, {200, 2}]; chull = ConvexHullMesh[pts]; styles = MapThread[Style, {{0, 1, 2}, {Red, Green, Yellow}}]; fullmesh[bm_] := MeshRegion[MeshCoordinates[bm], ...


3

Here is a workaround that's easy enough since it makes use of the already created Mesh region: Graphics[GraphicsComplex[ MeshCoordinates[chull], {Green, MeshCells[chull, 1], Red, PointSize[0.02], MeshCells[chull, 0], Opacity[0.6], Yellow, MeshCells[chull, 2]}]]


2

You have a good answer already, but I'll mention the following since it may be useful in the future. Many built-in functions are written in MATLAB and can be viewed, in this case edit nextpow2.m brings up the source for this function, which can be used as a starting point to implement in Mathematica.


2

rcollyer pointed out in a comment that the the new GroupBy may be substituted for GatherBy in Szabolcs's original to produce the desired function: cleanPosIdx[x_] := GroupBy[Range @ Length @ x, x[[#]] &] I shall be using this code until PositionIndex receives an enhancement.


2

Compile: TensorRank: { {{2,3},{1,2.},{1,1.}}, {{2,3.},{1,2},{1,1}}, {{2,3},{1,2},{1,1}} } // TensorRank 3 Therefore: cfn = Compile[{{p, _Real, 3}}, Plus @@@ p]; {{{2, 3}, {1, 2.}, {1, 1.}}, {{2, 3.}, {1, 2}, {1, 1}}, {{2, 3}, {1, 2}, {1, 1}}} // cfn {{4., 6.}, {4., 6.}, {4., 6.}} If you are using an older version of Mathematica use ...


2

Here a very direct approach to the problem: list1 = Subsets[{p1, p2, p3}, {2}]; f = Module[{x1 = #[[1, 1]], y1 = #[[1, 2]], x2 = #[[2, 1]], y2 = #[[2, 2]]}, If[x1 > x2 && y1 < y2, #[[1]], #[[2]]] ] &; and then Tally[f /@ list1] However, I'm not sure if the alternative is correct, as it wasn't specified. If you ...


1

This question is going to be closed as a duplicate. But let's just see where you went wrong: Join[a,b] is not {a,b}, you have to type Join[{a},{b}] if you want to use Join in this way. The second argument of Select is a criterion function, it needs to return either true or false. Your function returns a list of boolean values instead. You need to check if ...


1

Here you code Select[Range@5000,(PrimeQ[#]&&PrimeQ[FromDigits[Reverse[IntegerDigits[#]]]])&]


1

Assuming g is the only variable in the second position and any thing before is a constant: ClearAttributes[Times, Orderless]; func[f_, a__ g_] := a func[f, g] func[f, 3 g] func[f, 3 Pi g] func[f, 3 Pi a^3 g] (*3 func[f, g]*) (*3 \[Pi] func[f, g]*) (*3 \[Pi] a^3 func[f, g]*)



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