## Simplifying expressions with unit'd values

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Simplifying expressions with unit'd values

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)))

Flumble
Yes Man
Posts: 2261
Joined: Sun Aug 05, 2012 9:35 pm UTC

### Re: Simplifying expressions with unit'd values

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?

Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Re: Simplifying expressions with unit'd values

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

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.

Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Re: Simplifying expressions with unit'd values

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)))

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

### Re: Simplifying expressions with unit'd values

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.

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

### Re: Simplifying expressions with unit'd values

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.

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 + ...]

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

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...

Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Re: Simplifying expressions with unit'd values

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)))

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

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.

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.

Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Re: Simplifying expressions with unit'd values

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

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?

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

### Re: Simplifying expressions with unit'd values

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.

Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Contact:

### Re: Simplifying expressions with unit'd values

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)))