Simplifying expressions with unit'd values

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Simplifying expressions with unit'd values

Postby Xanthir » Mon Apr 04, 2016 10:56 pm UTC

I'm dealing with arbitrary math expressions produced from unit'd values and +-*/. I'd like to be able to simplify the expressions into an equivalent sum of unit'd values, so there's no need to carry around an AST.

This is easy for expressions like "(1a + 2b)/3c" (where a, b, and c are units that can't be simplified further at the moment) - it turns into two values: 1/3 with a unit of a/c, and 2/3 with a unit of b/c.

Is it possible to similarly simplify an expression like "1a/(2b + 3c)"? I'm pretty sure I can't, but I also can't find any useful discussion from Googling - best I can find is expressions where the denominator is something like "3 + √11", where you can just use the conjugate to transform the √ into an integer and combine the terms. Combining terms isn't an option here.

If I'm allowed "invert" as a primitive operator, I can simplify it to "inv(2(b/a) + 3(c/a))", but I'm curious if it's possible without that.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Flumble
Yes Man
Posts: 2264
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Simplifying expressions with unit'd values

Postby Flumble » Mon Apr 04, 2016 11:49 pm UTC

I guess it's only possible if a can be expressed as a function of b and c, in which case you can try (an analogue of) fraction decomposition.

Maybe it's easier to store a sequence of operations (or actual opcodes) to avoid parsing an AST?

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Simplifying expressions with unit'd values

Postby Xanthir » Tue Apr 05, 2016 12:11 am UTC

Context is that the CSS calc() function currently only allows */ with numbers as at least one of the operands, so you can't do unit algebra, but the CSSWG has it on record that we plan to expand it in the future. I'm also developing a new object-based OM for CSS, tho (as opposed to the current string-based one), and currently calc() is represented as a simple object with all the units as properties, so you can represent, say, calc(10px + 2em) as {px:10, em:2}. More complex expressions can always be simplified to this form.

Unit algebra complicates things, tho, and I'm trying to limit how complex it can get. Right now I'm thinking we'll have to expand it to the current structure, for simple-unit things, plus a list of complex-unit things (where each entry is a value + some way of expressing the unit), plus a list of inverses, where each inverse is a list of complex-unit things.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Cauchy
Posts: 602
Joined: Wed Mar 28, 2007 1:43 pm UTC

Re: Simplifying expressions with unit'd values

Postby Cauchy » Tue Apr 05, 2016 8:39 am UTC

You can always reduce any complex expression to the ratio of two polynomials, but you won't be able to go further than that, except in the case that the numerator and denominator share a common factor, or in the case that the denominator is a monomial.
(∫|p|2)(∫|q|2) ≥ (∫|pq|)2
Thanks, skeptical scientist, for knowing symbols and giving them to me.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Simplifying expressions with unit'd values

Postby Xanthir » Tue Apr 05, 2016 6:18 pm UTC

All right, cool, that answers my question, even if it's unfortunate.

So yeah, I'm stuck a list of values with (possibly complex) units, and a list of *inverses* of lists of values with (possibly complex) units. Ok.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Carmeister
Posts: 24
Joined: Fri Jan 24, 2014 5:10 am UTC

Re: Simplifying expressions with unit'd values

Postby Carmeister » Thu Apr 07, 2016 8:55 pm UTC

I don't know what context this is in, but I just want to point out that in general we don't allow adding two quantities unless they have the same units.

User avatar
cyanyoshi
Posts: 418
Joined: Thu Sep 23, 2010 3:30 am UTC

Re: Simplifying expressions with unit'd values

Postby cyanyoshi » Thu Apr 07, 2016 9:17 pm UTC

I am not sure it would count as "simplifying", but you can also represent fractions as power series. Ignoring annoying issues such as convergence, there is the following formula for a geometric series.
Image
Using this result, you can rearrange things around to represent a/(2b+3c) as one of several infinite series, like this one where x=-(3c)/(2b).

a/(2b+3c) = a/(2b) * 1/(1-x)
= a/(2b)*[1 + x + x2 + ...]

User avatar
chridd
Has a vermicelli title
Posts: 846
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Simplifying expressions with unit'd values

Postby chridd » Thu Apr 07, 2016 11:15 pm UTC

Carmeister wrote:I don't know what context this is in, but I just want to point out that in general we don't allow adding two quantities unless they have the same units.
If I understand correctly, this is to allow computations involving both absolute lengths (like inches and centimeters) and relative lengths (like em, which is based on the current font size, and %, which is based on the width or height of the containing element).

Obviously, calculations involving multiple absolute units can be simplified (1mm + 1cm = 11mm; 1cm + 1in = 3.54in), although from looking at the document linked it looks like they aren't for some reason?

(Will things like opacity:calc(5in/100%) be allowed?)
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Simplifying expressions with unit'd values

Postby Xanthir » Fri Apr 08, 2016 12:17 am UTC

chridd wrote:
Carmeister wrote:I don't know what context this is in, but I just want to point out that in general we don't allow adding two quantities unless they have the same units.
If I understand correctly, this is to allow computations involving both absolute lengths (like inches and centimeters) and relative lengths (like em, which is based on the current font size, and %, which is based on the width or height of the containing element).]

Right. These are units that are comparable *eventually* - later stages in the CSS value pipeline transform units into a particular "absolute" unit - for lengths, everything eventually turns into px, etc.

But in earlier stages, those units aren't combinable - you can't combine 10px and 1em into something simpler until you know how large an em is.

Obviously, calculations involving multiple absolute units can be simplified (1mm + 1cm = 11mm; 1cm + 1in = 3.54in), although from looking at the document linked it looks like they aren't for some reason?

Preserving author intent - if you say "width: 1in" we don't turn that into "width: 96px" immediately, even tho the two are equivalent; calc() similarly keeps the units as-specified until later stages of the value pipeline.

(Will things like opacity:calc(5in/100%) be allowed?)

Nope - in 'opacity', %s are resolved against a <number> (specifically, the number 1), so that's the type they represent. Dividing a <length> by a <number> yields a <length> as the type of the expression, and <length>s are invalid in 'opacity'.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Simplifying expressions with unit'd values

Postby Yakk » Fri Apr 08, 2016 2:13 pm UTC

cyanyoshi wrote:I am not sure it would count as "simplifying", but you can also represent fractions as power series. Ignoring annoying issues such as convergence, there is the following formula for a geometric series.
Image
Using this result, you can rearrange things around to represent a/(2b+3c) as one of several infinite series, like this one where x=-(3c)/(2b).

a/(2b+3c) = a/(2b) * 1/(1-x)
= a/(2b)*[1 + x + x2 + ...]

That doesn't seem to be directly useful.

But having units of:
a/(2b) * (1/1+(3c/2b))
might be. It looks vaguely continued-fraction-y.

Unfortunately it is also:

a/(3c) * (1/1+(2b/3c))

and it may not be possible to always decompose stuff into something like this, and it isn't unique, so probably not useful.

OTOH, the power series does tell us that 1-1/x is a decent approximation for (1/(1+x)) so long as x is reasonably large. I could see that knowledge being useful somehow in layout.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Simplifying expressions with unit'd values

Postby Xanthir » Fri Apr 08, 2016 2:29 pm UTC

OTOH, the power series does tell us that 1-1/x is a decent approximation for (1/(1+x)) so long as x is reasonably large. I could see that knowledge being useful somehow in layout.

Unfortunately, it's not. There's no significant difference in calculation time between those two.

But having units of:
a/(2b) * (1/1+(3c/2b))
might be. It looks vaguely continued-fraction-y.

Unfortunately it is also:

a/(3c) * (1/1+(2b/3c))

Point is to eliminate the multiplication entirely - I'd simplify it to 1/(2b/a + 3c/a). (Remember the letters are units, not quantities, so they don't count as mult/div on their own.)
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Derek
Posts: 2181
Joined: Wed Aug 18, 2010 4:15 am UTC

Re: Simplifying expressions with unit'd values

Postby Derek » Mon Apr 11, 2016 2:52 am UTC

What is the meaning of units in the denominator in this case? Can you provide an example of how this would be used and what it would translate to physically?

User avatar
PM 2Ring
Posts: 3715
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Simplifying expressions with unit'd values

Postby PM 2Ring » Mon Apr 11, 2016 6:35 am UTC

Derek wrote:What is the meaning of units in the denominator in this case? Can you provide an example of how this would be used and what it would translate to physically?


We need ratios of lengths if we want to be able to scale things and ensure the scaled version is a similar shape ( in the sense of similar triangles) to the original. Probably the most important application for Xanthir's purposes is to preserve the aspect ratio of rectangular elements like frames and canvases, and of course we also want to preserve the shape of any contents of those frames and canvases. :)

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Simplifying expressions with unit'd values

Postby Xanthir » Tue Apr 12, 2016 12:53 am UTC

Actually, calc() can't do aspect-ratio in any way! The best you can do (if unit division is allowed) is to match the aspect ratio *of the screen*, by dividing vw and vh.

But yeah, unit ratios are useful for various things. Sass libraries have some interesting examples. Converting things across units is also sometimes interesting, like mapping lengths to angles for hsl().
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))


Return to “Mathematics”

Who is online

Users browsing this forum: No registered users and 10 guests