Comparing units

7 views
Skip to first unread message

Ted Kosan

unread,
Jan 16, 2019, 1:55:45 AM1/16/19
to mathpi...@googlegroups.com
Kory,

How hard do you think it would be to add support for using comparison operators with units?:

In> 1~ms >? 2~ms
Result: 2~ms <? 1~ms

In> 12~inch =? 1~foot
Result: False

Ted

Kory Byrne

unread,
Jan 16, 2019, 3:45:32 PM1/16/19
to mathpiper-dev
Hey Ted,

I got the following to work in certain cases, however I don't understand why the failures are happening. Perhaps a precedence issue for some of them?

%mathpiper
unitComparisonConversions
:= [(a_ ~ u_) <?  (b_ ~ v_) <- ValueOf(a ~ u # v) <?  b,
                             
(a_ ~ u_) <=? (b_ ~ v_) <- ValueOf(a ~ u # v) <=? b,
                             
(a_ ~ u_) =?  (b_ ~ v_) <- ValueOf(a ~ u # v) =?  b,
                             
(a_ ~ u_) >=? (b_ ~ v_) <- ValueOf(a ~ u # v) >=? b,
                             
(a_ ~ u_) >?  (b_ ~ v_) <- ValueOf(a ~ u # v) >?  b];
                             
aFoot      
:= 1~ft;
almostAFoot
:= 11~inch;
anotherFoot  
:= 12~inch;

Verify(1~ft >? 11~inch       /:: unitComparisonConversions, True);
Verify(aFoot >? almostAFoot  /:: unitComparisonConversions, True);
Verify(1~ft <? 11~inch       /:: unitComparisonConversions, False);
Verify(aFoot <? anotherFoot  /:: unitComparisonConversions, False);

Verify(1~ft >=? 11~inch      /:: unitComparisonConversions, True); //Not?(False) weird fail
Verify(aFoot >=? almostAFoot /:: unitComparisonConversions, True);

Verify(1~ft =? 11~inch      /:: unitComparisonConversions, False);
Verify(aFoot =? almostAFoot /:: unitComparisonConversions, False);

Verify(1~ft =? 12~inch      /:: unitComparisonConversions, True);        // Fails!
Verify(aFoot =? anotherFoot /:: unitComparisonConversions, True);
Verify(` '(@aFoot =? @anotherFoot) /:: unitComparisonConversions, True); // Passes!

%/mathpiper

    %output,mpversion=".267",preserve="false"
      Result: True
     
      Side Effects:
      ******************
      File: LOADSCRIPT_EVALUATE_USER, Line: 16
     
      1~ft >=? 11~inch /:: unitComparisonConversions
       evaluates to
      Not?(False)
       which differs from
      True
      ******************
      ******************
      File: LOADSCRIPT_EVALUATE_USER, Line: 17
     
      aFoot >=? almostAFoot /:: unitComparisonConversions
       evaluates to
      Not?(False)
       which differs from
      True
      ******************
      ******************
      File: LOADSCRIPT_EVALUATE_USER, Line: 22
     
      1~ft =? 12~inch /:: unitComparisonConversions
       evaluates to
      False
       which differs from
      True
      ******************
      ******************
      File: LOADSCRIPT_EVALUATE_USER, Line: 23
     
      aFoot =? anotherFoot /:: unitComparisonConversions
       evaluates to
      False
       which differs from
      True
      ******************
     
.   %/output


Ted Kosan

unread,
Jan 17, 2019, 1:27:26 AM1/17/19
to mathpi...@googlegroups.com
Kory,

I will have time to look into this problem on Thursday evening.

Ted

--
You received this message because you are subscribed to the Google Groups "mathpiper-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathpiper-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ted Kosan

unread,
Jan 17, 2019, 11:16:49 PM1/17/19
to mathpi...@googlegroups.com

Kory,

The global rule evaluator evaluates the operands that are passed to "/::", and the result of this evaluation are what is checked by the local rules. For example:

In> 1~ft >=? 11~inch
Result: Not?(1~ft <? 11~inch)

Look at line 87 in "src/org/mathpiper/scripts4/stubs/comparison_operators.mpws" to see where the "Not?" is coming from.

The reason I created versions of the commonly used operators that don't evaluate their operands (these operators end with a "$") is to provide the ability to shield expressions from being completely evaluated by the global evaluator:

In> 1~ft >=?$ 11~inch
Result: 1~ft >=?$ 11~inch

These kind of expressions can then be processed by a set of local rules;

%mathpiper
unitComparisonConversions := [(a_ ~ u_) <?$  (b_ ~ v_) <- ValueOf(a ~ u # v) <?  b,
                              (a_ ~ u_) <=?$ (b_ ~ v_) <- ValueOf(a ~ u # v) <=? b,
                              (a_ ~ u_) =?$  (b_ ~ v_) <- ValueOf(a ~ u # v) =?  b,
                              (a_ ~ u_) >=?$ (b_ ~ v_) <- ValueOf(a ~ u # v) >=? b,
                              (a_ ~ u_) >?$  (b_ ~ v_) <- ValueOf(a ~ u # v) >?  b];

                            
aFoot       := 1~ft;
almostAFoot := 11~inch;
anotherFoot  := 12~inch;

Verify(1~ft >?$ 11~inch             /:: unitComparisonConversions, True);
Verify(`(@aFoot >?$ @almostAFoot)   /:: unitComparisonConversions, True);
Verify(1~ft <?$ 11~inch             /:: unitComparisonConversions, False);
Verify(`(@aFoot <?$ @anotherFoot)   /:: unitComparisonConversions, False);

Verify(1~ft >=?$ 11~inch            /:: unitComparisonConversions, True); //Not?(False) weird fail
Verify(aFoot >=?$ almostAFoot       /:: unitComparisonConversions, True);

Verify(1~ft =?$ 11~inch             /:: unitComparisonConversions, False);
Verify(aFoot =?$ almostAFoot        /:: unitComparisonConversions, False);

Verify(1~ft =?$ 12~inch             /:: unitComparisonConversions, True); // Fails!
Verify(aFoot =?$ anotherFoot        /:: unitComparisonConversions, True);
Verify(` '(@aFoot =?$ @anotherFoot) /:: unitComparisonConversions, True); // Passes!


%/mathpiper

    %output,mpversion=".267",preserve="false"
      Result: True
.   %/output

However, in this case, I think it would be better to use global rules to compare units instead of trying to use local rules. Most of these global unit comparison rules look like they would go in "comparison_operators.mpws". However, since "=?" is implemented in Java code, it looks like the unit comparison logic for it will need to be placed in the "equals" method that is in "src/org/mathpiper/lisp/Utility.java".

Ted


--
Reply all
Reply to author
Forward
0 new messages