New answers tagged

15 votes
Accepted

Association vs Pair - Creation performance, Selection performance

Simply use keys = Range[1000000]; vals = Range[1000000]^2; result = Pick[vals, PrimeQ[keys]]; to get the best of both worlds (zero construction cost and even ...
user avatar
0 votes

How to Create an Efficient Word Square Algorithm?

I have created an algorithm that takes about 2.4 seconds to find the word squares for "hair" and 10.5 seconds for "dogs". ...
user avatar
5 votes
Accepted

Replacement, PatternTest, Module related Question

The reason your attempt fails is that at the time the Function is applied to its arguments, m has not yet been replaced by ...
user avatar
  • 23.2k
5 votes
Accepted

Classifying elements, by the value of a fixed function, using Replace

L = Table[ConstantArray[1, RandomInteger[999]], 100000]; m = Replace[L, {x_List :> Quotient[Length@x, 100]}, {1}]; $$\{7,2,8,8,6,5,1,5,2,1,1,3,6,7,2,4,7,2,1,8,...
user avatar
  • 14.2k
6 votes
Accepted

While inside a function body does not work as expected

You may wish to disambiguate your description as it seems you're creating rational functions made up of polynomials. Below I modularized CreateDiscontinuityPoly ...
user avatar
  • 1,084
1 vote
Accepted

get positions satisfying multiple criteria

Granted that your example may not be actually representative of what you ultimately want, here's an attempt: ...
user avatar
  • 3,452
0 votes

How to remove duplicates from a pair with some conditions?

How about listNoDuplicates = DeleteDuplicates[Sort /@ list]? Each list inside list gets sorted with ...
user avatar
  • 1,393
2 votes

How to remove duplicates from a pair with some conditions?

Here's a stab, but I suspect it can be made more elegant. I'll walk through the development step by step before putting together a complete function. ...
user avatar
  • 3,452
11 votes

How can I use variables instead of slots in DeleteCases?

Pick could also be useful: Pick[tuples, Function[{a, b, c}, a + 2 b + c == 0] @@@ tuples, False]
user avatar
  • 4,809
8 votes

How can I use variables instead of slots in DeleteCases?

g[{a_, b_, c_}] := a + 2 b + c == 0 Using the operator form: DeleteCases[x_ /; g[x]][tuples]
user avatar
  • 14.2k
11 votes
Accepted

How can I use variables instead of slots in DeleteCases?

...
user avatar
  • 51.3k
8 votes

How can I use variables instead of slots in DeleteCases?

Perhaps (Thanks to comment/correction @BenIzd ) DeleteCases[tuples, _?(Apply@Function[{a, b, c}, a + 2 b + c == 0])] is what you are looking for?
user avatar
1 vote

How to generate all commutative pairings of list elements?

Here is one more recursive answer that I find slightly easier to read: ...
user avatar
  • 2,791
5 votes
Accepted

How to simplify function with Abs?

PiecewiseExpand does it. Edit and // ComplexExpand // Simplify does it ...
user avatar
  • 15.1k
5 votes

How to simplify function with Abs?

Here's the ComplexityFunction way I referred to in my comment just to illustrate the alternative: ...
user avatar
  • 213k
6 votes

How to simplify function with Abs?

Try this: Refine[Simplify[mylist], Assumptions -> x \[Element] Reals]
user avatar
5 votes
Accepted

Tallying over columns of matrix

Not sure what would be the best representation in your case, but here's a possible way to start. Let's start with a simplified case of one isolated column (as a List): ...
user avatar
  • 3,452
4 votes

How to test a recursive formula using Mathematica?

For a fully recursive implementation, for which @kenric gives an outline, the definition must be inductive. In particular, you need a base case (or more in some cases, like for the Fibonacci recursion)...
user avatar
  • 213k
3 votes
Accepted

RunProcess : Save directly to Mathematica Variable

It might be possible to do what you say, if myprogram.exe can output the image data directly to stdout. But I wouldn't lose any sleep over having to create a ...
user avatar
  • 62.4k
4 votes

How to find palindromic numbers (Project Euler #4)?

This is a report on an upgrade in later versions. Things changed for the best with regards to this question. Since V10.3 we have ...
user avatar
  • 5,684
1 vote

How can I efficiently define functions with different names but doing the same stuff?

If I understand your question correctly, this is how I usually implement this. ...
user avatar
  • 1,180
1 vote

How can I efficiently define functions with different names but doing the same stuff?

Just to show another perspective that could also work on multiple definitions. Define your base function (which could have multiple definitions) ...
user avatar
  • 4,809
4 votes
Accepted

How can I efficiently define functions with different names but doing the same stuff?

To define identical functions with SetDelayed use (#[x_] := x^2 + x) & /@ {f, g, h}; This approach is also useful when ...
user avatar
  • 119k
7 votes

How can I efficiently define functions with different names but doing the same stuff?

You could do something like this: ...
user avatar
  • 3,452

Top 50 recent answers are included