Flatten Mathematica

0 views
Skip to first unread message

Marcelene Vasconez

unread,
Aug 3, 2024, 5:44:30 PM8/3/24
to hucvatantweb

Motivation:Compile can only handle full arrays (something I just learned -- but not from the error message), so the idea is to use unflatten together with a compiled version of the flattened expression:

Example of a solution to a less general problem:What I actually need to do is to calculate all the derivatives for a given multivariate function up to some order. For this case, I hack my way along like so:

I am not sure what you are trying to do with Compile. It is used when you want to evaluate procedural or functional expressions very quickly on numerical values, so I don't think it is going to help here. If repeated calculations of D[f,...] are impeding your performance, you can precompute and store them with something likeTable[d[k]=D[f,x,y,k],k,0,kk];

It might seem a little like a cheat to use the original expression in the function, but as aaz points out, we need some information from the original expression. While you don't need it all, in order to have a single function that can unflatten, all is necessary.

My application is similar to Janus's: I am parallelizing calls to Simplify for a tensor. Using ParallelTable I can significantly improve performance, but I wreck the tensor structure in the process. This gives me a quick way to reconstruct my original tensor, simplified.

Something I've wondered about a couple of times is how to "flatten" graphics in Mathematica. I don't know if this is really the right terminology, but the idea is that I have a bunch of semi-transparent objects inside a Graphics object. They are visually layered from bottom to top based on the order the appear inside Graphics, but I would like merge them down so that they're all in the same visual layer.

Now, the problem with the image is that the individual 9-gons are not all visually at the same level. For example, the last entry in the table is the reddish 9-gon just below 3 o'clock in the image which clearly lies above all the other 9-gons. The question is: how do I get the final image to appear flat?

A minor problem is that ImageMultiply darkens all the colors (which could probably be gotten around by careful fiddling), but the big problem is that there's no way to do this trick with a colored or transparent background.

The best solution I've come up with so far is to use Blend. Basically, I make a list where each entry in the table is a Graphics object with one missing 9-gon and a transparent background, apply Blend to the whole list, and then ImageCompose with a blank image with the background color I want (of course, I first just had a list with only one 9-gon in each entry, but this gets way too washed out when you apply Blend).

This weekend, I made Peter Reinhart's water bagel recipe from The Bread Baker's Apprentice. The bagels taste yummy, but they're flat wrinkly discs! What can I do next time to get nice puffy bagels instead?

Per Johanna's diagnosis of overproofing, I tried halving the yeast and then otherwise making the bagels as I did above (just with a bit of extra rise time for the sponge), and they turned out so much better!!

Bagels tend to flatten when you remove them from the water if the dough is overproofed or you boiled them for too long. Next time, let them proof for shorter time in the fridge (I find that doughs get overproofed in the refrigerator after about 12 hours, so 24 hours is a very long cold proof) and possibly boil them slightly shorter.

Besides Johanna's overproofing hypothesis (which I find very likely), it seems you boiled them straight from the fridge. I would never do that, dough behaves much better when it is allowed a long warm up step after having been in the fridge.

I don't think the gluten content of the flour contributed anything to the deflation. While bagels are indeed made with high-gluten flour (actually higher than bread flour, there is a table in Bread Baker's apprentice I believe, check it out), you can get perfectly good rise out of all-purpose flour, and many breads are baked with all-purpose flour. Of all the probable causes you are listing, only the first one - added yeast - sounds like it can cause these symptoms. The more yeast you add, the less time you have before it overproofs.

BaCon has the concept of delimited strings, which may contain delimited strings within delimited strings etc. Such nested delimited strings must be surrounded by (escaped) double quotes in order to avoid their delimiter messing up operations on higher level delimited strings. However, from functional point of view, a delimited string is the same as a regular list. The special function FLATTEN$ can actually flatten out lists within lists. The last SORT$ in the program below makes sure no empty items remain in the list.

Since C++ currently doesn't have nice syntax for initializing lists, this includes a simple parser to create lists of integers and sublists. Also, there's no standard way to output this type of list, so some output code is added as well.

Fortran does not offer strings, only CHARACTER variables of some fixed size. Functions can return such types, but, must specify a fixed size. Or, mess about with run-time allocation as above. Since in principle a list is arbitrarily long, the plan here is to crush its content in place, and thereby never have to worry about long-enough work areas. This works because the transformations in mind never replace something by something longer. A subroutine can receive an arbitrary-sized CHARACTER variable, and can change it. No attempt is made to detect improper lists.

Note that if you insist on the rather flabby style of having spaces after commas, then there would be trouble. Instead of placing just a comma, a ", " would be required, which is two symbols going out when one symbol has come in: overwriting yet-to-be-scanned input is a bad idea. Either a more complex set of scan states would be required to squeeze in the extra or a separate work area would be needed to hold such output and the issue of "long enough" would arise.

All of this relies on the list being presented as a flat text, which text is then manipulated directly. If the list was manifested in a data structure of some kind with links and suchlike, then tree-traversal of that structure would be needed to reach the leaf entries.

In the code above, flatten uses an easy-to-read type switch to extract ints and return an int slice. The version below is generalized to return a flattened slice of interface type, which can of course refer to objects of any type, and not just int. Also, just to show a variation in programming style, a type assertion is used rather than a type switch.

We do not use ]S:0 (which puts everything zero levels deep) because it assembles its results as items of a list, which means that short items will be padded to be equal to the largest items, and that is not what we would want here (we do not want the empty item to be padded with a fill element).

The flatten method was overloaded for better separation of concerns. On the first one you can pass any List and get it flat into a LinkedList implementation. On the other one you can pass any List implementation you like for both lists.

Note that both implementations can only put the result into type List. We cannot type-safely put the result into a generic type List because there is no way to enforce that the original list contains elements of "type T or lists of elements which are T or further lists..."; there is no generic type parameter that will express that restriction. Since we must accept lists of any elements as an argument, we can only safely put them in a List.

Lasso Delve is a Lasso utility method explicitly for handling embedded arrays. With one array which contain other arrays, delve allows you to treat one array as a single series of elements, thus enabling easy access to an entire tree of values. www.lassosoft.com/lassoDocs/languageReference/obj/delve Lasso reference on Delve

This is distinctly crude. A user-written Replace function is confronted by the requirement to specify a maximum size for its returned result, for instance Replace:Procedure(text,that,this) Returns(Character 200 Varying); which is troublesome for general use. The intrinsic function Translate has no such restriction.

And, as the idea of Rosetta Code is to demonstrate how languages are similar as well as different, and to thus to 'aid a person with a grounding in one approach to a problem in learning another', here it is in terms of concatMap, which can be defined in any language, including mathematics, and which can be variously expressed in Python. (The fastest Python implementation of the concat component of the (concat . map) composition seems to be in terms of itertools.chain).

Note that because lists are not syntactically distinct from strings, it is probably a mistake to use this procedure with real (especially non-numeric) data. Also note that there are no parentheses around the outside of the list when printed; this is just a feature of how Tcl regards lists, and the value is a proper list (it can be indexed into with lindex, iterated over with foreach, etc.)

In the final stages of finishing a room in our walk-out basement. This particular room has a long history, as it was originally a garage. Thus, it has a slight pitch from the back of the room towards the wall that once contained the garage door (which is now a sliding glass door).

Unfortunately, there is a ridge down the center of the room where the concrete bows up about 3/8" of an inch over a 2' wide strip. I believe that this kind of ridge will cause issues when laying the laminate flooring.

My initial thought was to use self-leveling compound. However, being self-leveling, it will run down and pool quite thick at the front of the room, and require a significant amount to do (and might even go higher than the threshold of the sliding glass door).

I am unsure as to what type of product I could use that would be trawl on, and thick enough not to run, yet bond well with the underlying slab. Any thoughts on the best way to flatten a concrete floor that has a pitch in one direction that must be maintained?

c80f0f1006
Reply all
Reply to author
Forward
0 new messages