Menu

#56 SUBST(C;V): some improvements

Next_Release
open
nobody
None
1
2021-04-05
2018-08-17
No

Hi Jan!

It is now possible for SUBST(C) to accept a vector as a parameter like this: SUBST (@label@, x={1:100}). Also we have SUBSTV, which accepts vectors as lists of values (what's the difference?). But none of them can accept a list of vectors to substitute them independently.

My example is the same as from ITERATE ticket. [#23]. E.g. I have a substitution equation y = r * sin t. I've found a list of pairs (r,t) and I need to substitute it back to this equation to find the list of y values. I need to substitute it to get a vector of (y = r * sin t) for each pairs of (r,t):

y= (y1;y2;y3...
y= (r1 * sin t1; r2 * sin t2; r3 * sin t3; ...)

There is no way to find the function r=f(t), I have only an implicit equation f(r,t)=0. So I cant just solve the equation. Also I know about non-commutative operations, but it is exactly what I DON'T need in this case.

1 Attachments

Related

Feature Requests: #23
Feature Requests: #58

Discussion

  • zeon_account_will_be_deleted

    So is it possible?

    I think that SUBSTV should be suitable for it. Now it can accept only one vector. Could you improve it?

     
  • zeon_account_will_be_deleted

    HI Jan!

    I'll try to explain my idea. This thread is also related to ITERATE, CHART, and maybe VALWITH, because all of them have limited support of vectors. And this is long, sorry.

    Now SUBSTV can accept only expressions as the first parameter. If it would take equations and handle them the same way as VAL(@label@), it will be much comfortable to create charts. I suppose two cases.

    1. If @Label@ is an equation of type y = f(x) (single variable on the left), then
      EQDEF substv (@Label@ ; x={x1;x2;x3;...})
      should be a shortcut for this:
      EQDEF lhs(@Label@) = substv (rhs(@Label@) ; x={x1;x2;x3;...})
      and the result is:
      y = { f(x1) ; f(x2) ; f(x3) ; ... },
      where y becomes a list or a vector. We may use it in CHART or in other equations.

    2. If @Label@ is any other type of equation, in general g(x) = f(x) (something on the left and something on the right) possibly it should work like this:
      EQDEF substv (@Label@ ; x={x1;x2;x3;...})
      should be:
      EQDEF substv (lhs(@Label@) ; x={x1;x2;x3;...}) = substv (rhs(@Label@) ; x={x1;x2;x3;...})
      and the result is:
      { g(x1) ; g(x2) ; g(x3) ; ... } = { f(x1) ; f(x2) ; f(x3) ; ... }.

    In the last case we have an equation with several vectors which we can't use (I don't see any practical usage now except of a substitution equation). I mean that the equality {Vector1}={Vector2} is almost useless if we can't transform it to the equality of their elements, like this:
    { g(x1) = f(x1) ; g(x2) = f(x2) ; g(x3) = f(x3); ... = ...}
    OR in short
    { g1 = f1 ; g2 = f2 ; g3 = f3; ... = ...}

    BUT maybe I'm losing something in my logic. Maybe when a function accepts a vector it means something more than just f(x1;x2;x3;...)={f1;f2;f3;...). At least now iMath doesn't do evaluation to a numeric value for some functions, e.g. sin(x), if we have values of all elements of vector x.

    ITERATE: we could run several iterations in one string. One iteration for each pair. If you dont understand, I'll make an example for you.

    CHART: we could have an elegant way to create a graph of any pairs of points (any explicit or implicit function)

    VALWITH: see example

     

    Last edit: zeon_account_will_be_deleted 2018-08-17
    • zeon_account_will_be_deleted

      I'd like to clarify something.

      In the first example "Decart to polar - subst.odt" I have the iFormula called "Substitute x", where I substitue a vector of pairs {t,r} into the equation x=r cos t. To transform polar back to decart. So now it looks like this:
      %%ii @x@ EQDEF* x = {
      substc(rhs(@x:polar@), t=mindex(rhs(@t@);1;1); r=mindex(rhs(@r@);1;1));
      substc(rhs(@x:polar@), t=mindex(rhs(@t@);2;1); r=mindex(rhs(@r@);2;1));
      substc(rhs(@x:polar@), t=mindex(rhs(@t@);3;1); r=mindex(rhs(@r@);3;1));
      .... .... ... ... ;
      substc(rhs(@x:polar@), t=mindex(rhs(@t@);25;1); r=mindex(rhs(@r@);25;1))}

      Would like to do the same this way (no mindex, no RHS, noLHS):
      %%ii @x@ EQDEF* substv(@x:polar@, @t@; @r@)

      The same is with ITERATE, but it is another ticket. Sorry for so long text.

       

      Last edit: zeon_account_will_be_deleted 2018-08-18
      • Jan Rheinlaender

        Hi zeon,

        my suggestion for SUBSTV is like this:
        Also, we will need SUBSTCV as new keyword when we allow multiple equations.

         
    • Jan Rheinlaender

      Concerning the VAL(WITH) example, it's correct. sin() is not defined for vectors (not in mathematics and not in iMath).

      It would be possible to implement element-wise functions (similar to the Hadamard product and ./, .^) though I'm not quite sure what the syntax should be:

      • vsin() -> that requires defining a lot of new functions
      • vector sin() -> might be ambiguous
      • sin(v) -> (same syntax as for scalar sin, argument is recognized as vector and creates a vector of sin(v_1), sin(v_2) etc.). This would be ambiguous for functions which take a vector and evaluate it to a scalar (e.g. the trace of a matrix)
      • apply(sin(), {1:10:1}) -> similar to Mathematica
      • .sin (similar to ./ and .^ )

      I think it should be one of the two last possibilities.

       
      • zeon_account_will_be_deleted

        Hi, Jan!

        "sin(v) -> (same syntax as for scalar sin, argument is recognized as vector and creates a vector of sin(v1), sin(v2) etc.). This would be ambiguous for functions which take a vector and evaluate it to a scalar (e.g. the trace of a matrix)"

        This is what I mean. This is like applying the function to a list of arguments.

        How does the trace of matrix work now in iMath? As it takes only a square matrix ( 2 dimensional), there should not be any problem in this case. Or not?

         
        • Jan Rheinlaender

          Hi zeon!
          iMath has no trace function now. But it is possible for the user to define functions that work on vectors. So we MUST have two different syntaxes for functions which work on whole vectors/matrices and functions which just work sequentially on all elements of a vector.
          Think of the vector product. It is a function that takes two vectors and returns another vector. That's quite different to the Hadamard product.
          https://en.wikipedia.org/wiki/Vector_product
          https://en.wikipedia.org/wiki/Hadamard_product_(matrices)

           
  • zeon_account_will_be_deleted

    Hi Jan!

    I've found another issue with SUBSTV.

    SUBSCTV doesn’t find numerical values if they are defined indirectly. In some cases it is possible to solve it by a composition of VAL, SUBSTV, SUBSTC etc., but even this not always works. Seems to be logical, because SUBSTV just substitutes the vector into the equation. Maybe this is not a bug, but it's a shortcoming. Let it be feautre request.

     
    • Jan Rheinlaender

      Hi zeon!

      The first part is the same issue as before because sin() is not defined for vectors.

      The second is a bug in ifelse(). It is solved in next beta. For example:
      EQDEF r = ifelse(x<sin(x); "a"; "b")
      EQDEF VAL(SUBSTC(@r@, x=1))
      now evaluates correctly to "b".

      Thanks for finding the bug!

       
      • zeon_account_will_be_deleted

        Cool!

        But it still doesn't work in 2.2.7~beta. I mean your example above (without vectors)

         

        Last edit: zeon_account_will_be_deleted 2018-12-21
        • Jan Rheinlaender

          You're too fast. I ran out of time yesterday. Just now I uploaded beta2. You can test it now.

           
          • zeon_account_will_be_deleted

            Nice. it is working!

             
            • Jan Rheinlaender

              Can we close this ticket? See also #58 which continues the discussion about element-wise functions on vectors.

               

Anonymous
Anonymous

Add attachments
Cancel