Tag Info

New answers tagged

4

With 1. notes in the answer of your latter question, 2. initial values for FQout and RQout(the values are carelessly chosen real numbers because it's bound to be overlaid), the modified code is about 40 times faster now: f = With[{dx = 1/6, n = 48, m = 300, p = 36, capacity = 2500, A = 18., B = 0.1, L = 3., RML = 30, Vf = 100, Kj = 150, w = 20, ...


0

On the base of findings in linked thread the procedure is as follows. Assuming that residualVect[vars] is the residual vector where vars is a list of variables of complete (long) model, and we have a short model where one of the vars is fixed to numerical value. This fixed variable along with its value we will store as a rule in the variable set. Let us ...


3

Here is an old fashioned approach, using stacks. It is quite general in the sense that it will handle tree structures with more than two children per node. It is also fairly slow; it has the correct (linear) complexity but maybe an order of magnitude more pushing/popping than the length. SetAttributes[push, HoldRest]; SetAttributes[pop, HoldAll]; ...


1

Prompted by some conversation in comments elswhere, my method. Module[{o}, If[OrderedQ[#], Most@Accumulate@Prepend[Tally[#][[All, 2]], 1], o = Ordering[#]; o[[Most@Prepend[Accumulate[Tally[#[[o]]][[All, 2]]] + 1, 1]]]]] &[targetListHere] Can clobber GatherBy method by over an order of magnitude (e.g. on RandomInteger[1*^6,1*^5] my tests ...


4

Prompted by a comments conversation with Mr. Wizard, a method I use often. list = RandomInteger[1000, 100]; Module[{a, o, t}, Composition[o[[##]] &, Span] @@@ Pick[Transpose[{Most[Prepend[a = Accumulate[(t = Tally[#[[o = Ordering[#]]]]) [[All, 2]]], 0] + 1], a}], Unitize[t[[All, 2]] - 1], 1]] &[list] list[[#]] & /@ % (* ...


3

Original Bresenham I guess I can come of with a somewhat shorter implementation without using Reap and Sow. If someone is interested, it follows almost exactly the pseudo-code here bresenham[p0_, p1_] := Module[{dx, dy, sx, sy, err, newp}, {dx, dy} = Abs[p1 - p0]; {sx, sy} = Sign[p1 - p0]; err = dx - dy; newp[{x_, y_}] := With[{e2 = 2 err}, ...


9

Implementations Here is an "idiomatic" one: ClearAll[mapRec, reverse]; mapRec[f_, ll_] := Block[{$IterationLimit = Infinity}, reverse@mapRec[{}, f, ll]]; mapRec[accum_, _, {}] := accum; mapRec[accum_, f_, {head_, tail_}] := mapRec[{f[head], accum}, f, tail]; reverse[ll_] := reverse[{}, ll]; reverse[accum_, {}] := accum; reverse[accum_, {head_, tail_}] := ...


5

So far, the best I've been able to do uses Reap and Sow to construct an ordinary list while I traverse the linked list with a While loop. At the end, I reconstitute it using Fold in the normal way. It's pretty easy to capture the head of the list. It will fail (throw $Failed) if the list isn't terminated properly, or if it contains a head other than the head ...


3

I didn't read all of your question so I may misunderstand, but based on the example at the bottom I believe your operation is equivalent to this: sspos[list_] := {#[[1, 2]], #[[All, 1]]} & /@ ReplaceList[list, {a___, x__, ___} :> {Length@{a} + 1, {x}} ] ~GatherBy~ Last ~Cases~ {_, __}; test = {3, 0, 1, 0, 3, 2, 2, 0, 3, 0, 1, 3, 0, 2, 3, 3, 0, ...


3

You can easily see that if the nearest approach of the two infinite lines does not fall within both segments then you must calculate the nearest distance of all four end points to the other segment. pointsegdis[{seg_, pointlist_}] := Module[{u = Subtract @@ seg, mean = Plus @@ seg/2}, {#, (mean - u Sign@# Min[1, Abs@#]/2) &@(-(2 ( # - ...


0

One possible answer would be to try and reconstruct your problem analysis from the code you gave. I will not attempt that here. Rather, I'll start from your statement "..a set of numbers will be considered as a (sub)sequence, if it appears at least twice in (sic) and if its is length is greater than 1". That sounds like searching for subsequences of length 2 ...


1

Another way of doing this (http://mathforum.org/library/drmath/view/51980.html) is to find the mutual perpendicular between the two lines using the cross product, converting this to a unit vector, and then using the dot product between that cross product, and any vector going between the two lines. Like this: newMinDist[{p1_, p2_}, {q1_, q2_}] := Module[ ...


0

Suppose the equation of two lines are (x-x1)/A1=(y-y1)/B1=(z-z1)/C1 (x-x2)/A2=(y-y2)/B2=(z-z2)/C2 Then the oriented distance between them is Det[{{x2-x1,y2-y1,z2-z1},{A1,B1,C1},{A2,B2,C2}}]/Norm[Cross[{A1,B1,C1},{A2,B2,C2}]] You may need Abs to get the distance


1

An extended comment. I'm not sure if this has been realized, please correct me if it has. The result of the Divide[a,b] operation is not the same as the first 3 which are identical. {a, b} = List @@ RandomReal[{-50, 50}, {2, 1*^7}]; x1 = a/b; x2 = a b^-1; x3 = a/b; x4 = Divide[a, b]; Now... Tally[x1 - x2] Tally[x2 - x3] Both give 10^7 zeros. ...


1

The link you gave shows how to find the distance between any two points on the lines. You can then use Mathematica's Minimize function to find the shortest distance. Like this. minDist[{p1_, p2_}, {q1_, q2_}] := Module[{P, Q, u, v, w}, u = Normalize[p2 - p1]; v = Normalize[q2 - q1]; P[s_] := p1 + s u; Q[t_] := q1 + t v; ...


2

The reason about change only the argument of the external function, the behavior changes may be related to the setting of "ExpressionOptimization" under CompilationOptions. There is a related function called OptimizeExpression under Experimental` context, which I believe, according to this post by Daniel Lichtblau, is the one used by Compile for expression ...


3

The main thing that I am trying to show is that you can use Accumulate and that almost all these functions are compilable. I hope it also shows when to use Table rather than Do, to avoid making unnecessary ConstantArrays. I personally find the use of Table in your code confusing. Of course it is nice to localise variables from time to time, which is also ...


6

You have 2 sources of inefficiency in your code. One is that you don't use the machine-precision for your dx (and therefore xls), and another one is that Band is not the fastest way to build a SparseArray object, and in the time-dependent case you have to build a new one for every time point. Here is the code which is on the same level of performance as ...


8

To create Experimental`NumericalFunction, one needs to evaluate Experimental`CreateNumericalFunction[vars, expr, dims] where vars is a list of arguments, expr - the expression from which the numerical function will be created, dims - the dimensions of the output matrix produced by this expression. If the output is scalar, then dims should be set to {}. It ...


2

After some significant time browsing related questions on this site as well as trial and error, I believe that I have managed to create a solution for what I was trying to accomplish. Taking the advice from Ariel I have essentially created my own custom array of checkboxes that do not experience any major delay when one of them is clicked. I have ...


-5

There is no possible known method to add subtract multiply or divide, using a microprocessor, with equal efficiency. If you look at the various hardware implementations of ALU implementations you will see radically different designs for each function that all have pros and cons. It's an open question in computer science that will make you a very rich person ...


0

Your question is whether you are doing something fundamentally wrong. My answer is that you are not doing anything wrong but that the behavior you are experiencing is normal for CheckboxBar. I think that there are several potential causes for the slow behavior of CheckboxBar when it is fed with too many items but the final effect is that it is not a ...


3

It occurred to me that this problem can be recast as an image processing one. I think this approach is different enough to warrant its own answer. I like the style much better. f3[array_List, ele_, dist_] := Image[array] ~Binarize~ {ele, ele} ~MaxFilter~ {0, dist} // Join @@ Pick[array, ImageData @ #, 1] & f3[ar, 5, 2] {10, 6, 5, 7, 3, 7, 5, ...


2

I got an error running your code in version 7 and I had to use FromDigits[Normal@#, 2] to fix it. Therefore I don't know if my comparative timings are meaningful but here is what I came up with: f2[array_, ele_, dist_] := SparseArray[Unitize[array - ele], Automatic, 1]["AdjacencyLists"] // Outer[Plus, Range[-dist, dist], #, 1] & // ...



Top 50 recent answers are included