People still doing the daily Christmas code puzzles? I got caught up yesterday and saw you have to get both parts within 30 minutes of release to make the high score board. I had a pleasant moment tonight with today's puzzle. I was out on the patio, typed it into Maximize pretty directly, and it worked. Took 27 secs.
Maximize[{Max[3 a - 3 b - c, 0] Max[3 b, 0] Max[4 c - 2 d,
0] Max[-3 a + 2 d, 0],
a + b + c + d == 100 && a >= 0 && b >= 0 && c >= 0 && d >= 0}, {a,
b, c, d}, Integers]
Out of curiosity, I wanted to time just brute forcing the 100 million cases. Took 110 secs.
best = 0; Do[
If[a + b + c + d == 100,
cur = Max[3 a - 3 b - c, 0] Max[3 b, 0] Max[4 c - 2 d,
0] Max[-3 a + 2 d, 0]; best = Max[best, cur];], {a, 0, 100}, {b,
0, 100}, {c, 0, 100}, {d, 0, 100}]; best
However, then just optimizing based on the summation constraint to only check the cases that sum to 100 reduced the time to less than 2 secs.
best = 0; Do[d = 100 - a - b - c;
cur = Max[3 a - 3 b - c, 0] Max[3 b, 0] Max[4 c - 2 d,
0] Max[-3 a + 2 d, 0];
best = Max[best, cur];, {a, 0, 100}, {b, 0, 100 - a}, {c, 0,
100 - a - b}]; best
So the behavior of Maximize falls somewhere between brute forcing and the first optimization a human would make. Not sure how much is due to processing optimizations like compilation vs method.
I suppose the key point from Wolfram's perspective is that it took me more than 30 seconds to think of the optimization, so Mathematica saved me time even if I was the fastest Python writer.
Does anyone know how to force ScientificForm to always give a digit after the decimal? I want ScientificForm[1.0, 2] to output 1.0 so that it looks better next to ScientificForm[0.82, 2]
@Kuba, that works for 1.0 but then it omits the exponent on 0.82 - sorry I wasn't clear, I'm looking for a labeling function for a legend, so one function that would do well for smaller and larger numbers. In the past I used a piecewise function, using NumberForm for numbers between 0.1 and 9.9 and ScientificForm for all others, but that feels clunky. I thought this was simple enough to ask here but maybe I should post it as a question.
Do the answers given in this post, mathematica.stackexchange.com/questions/30604/…, work for others using version 10? Particularly, I can only get PasteRaw[] to work if the contents of the clipboard were copied from Mathematica
Is there any way to get BoxObject, except with EvaluationBox[] | ParentBox?
So usually one need to create objects with Initialization or Buttons and use EvaluationBox[] etc.
I would love to see more flexible approach e.g. how to get it from current selection.
I was only able to find:
BoxO...
I have a large packaged integer array arr (of arbitrary dimensions). I also have a list of integers. I need to replace those elements of arr which appear in list with a 1. The rest should be replaced with 0. What is a fast way to do this?
I don't want to post a main site question because I already solved this: I programmed it in C. But I was wondering if there's a fast way to do it in pure Mathematica. The naive way I used amounts to 1 - Fold[# Unitize[arr - #2] &, ConstantArray[1, Length[arr]], list]. Let's assume for now that list is not too big.
@R.M. Yes and yes. I was just writing up a question but I got interrupted. The Fold version is linear in the size of list, the Lookup version would be no worse than logarithmic (I assume). So which one is faster depends on the size of list. But the Lookup/Association version I was able to come up with unpacks, and my arrays are huge.
I have a large integer packed array arr. My array is multidimensional, but for simplicity let's consider 1D arrays for now. ArrayReshape takes care of the rest.
I also have a set of integers (a list).
The task is to replace those elements of arr which are in set with 1, and those which aren't...
@Silvia overmodest, as usual ;-) BTW, some people got flak here because of use of Lena - at least in your case this would not be so critical, gender-wise :D
@halirutan Thanks for the answer! It seems that there might not be a much faster solution. I suspected that that is a case, but I have been wrong about this many times in the past! So I thought it's still better to ask and maybe learn something new.
@rcollyer I checked just now. The upgrade box still says 10.1, but when I click the upgrade button a download page for 10.3.1 opens for me. Downloading now.
Background Today, many helicopters launch from and land on ships at sea. Some are conventional helicopters, both commercial and military, and some are drones. In Wolfram SystemModeler, we now have a system for simulating helicopter landings and launches that includes waves and ships. The models have been used for the design of mechanical parts, autopilots, [...]