solving equation system

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TG

    #1

    solving equation system

    Hi there.

    Anyone knows how to use numpy / scipy in order to solve this ?

    * A is an array of shape (n,)
    * X is a positive float number
    * B is an array of shape (n,)
    * O is an array of shape (n,) containing only zeros.

    A.X - B = O
    min(X)

    thanks.

  • Ben C

    #2
    Re: solving equation system

    On 2006-07-17, TG <girodt@gmail.c omwrote:
    Hi there.
    >
    Anyone knows how to use numpy / scipy in order to solve this ?
    >
    * A is an array of shape (n,)
    * X is a positive float number
    * B is an array of shape (n,)
    * O is an array of shape (n,) containing only zeros.
    >
    A.X - B = O
    min(X)
    Are we solving for A, B or X? And what do you mean by min(X)?

    If we're solving for X there will be many combinations of A and B for
    which there is no solution.

    Comment

    • TG

      #3
      Re: solving equation system


      Ben C wrote:
      On 2006-07-17, TG <girodt@gmail.c omwrote:
      Hi there.

      Anyone knows how to use numpy / scipy in order to solve this ?

      * A is an array of shape (n,)
      * X is a positive float number
      * B is an array of shape (n,)
      * O is an array of shape (n,) containing only zeros.

      A.X - B = O
      min(X)
      >
      Are we solving for A, B or X? And what do you mean by min(X)?
      >
      If we're solving for X there will be many combinations of A and B for
      which there is no solution.
      Sorry for the poor explanation. I'm trying to put it clear now.

      i've got A and B. I'm looking for X. I made a mistake in my equation
      :-/

      It's more like :

      A.X - B >= O

      Well, maybe it will be much more simple if I explain the underlying
      problem :

      I have an array of N dimensions (generally 2).
      - A first calculation gives me a set of integer coordinates inside this
      array, which I will call the point W.
      - After several other calculations, I've got a set of coordinates in
      this N dimensional space that are floating values, and not bound to the
      limits of my original N-array. This is the point L.

      What I want to do is to translate the point L along the vector LW in
      order to get a point L' which coordinates are inside the original
      N-dimensional array. Then it will be easy to get the closest integer
      coordinates from L'.


      I'm not sure this is clear ... pretty hard to talk about maths in
      english.

      Comment

      • faulal@gmail.com

        #4
        Re: solving equation system


        TG wrote:
        Hi there.
        >
        Anyone knows how to use numpy / scipy in order to solve this ?
        >
        * A is an array of shape (n,)
        * X is a positive float number
        * B is an array of shape (n,)
        * O is an array of shape (n,) containing only zeros.
        >
        A.X - B = O
        min(X)
        >
        thanks.

        Comment

        • faulal@gmail.com

          #5
          Re: solving equation system


          TG wrote:
          Hi there.
          >
          Anyone knows how to use numpy / scipy in order to solve this ?
          >
          * A is an array of shape (n,)
          * X is a positive float number
          * B is an array of shape (n,)
          * O is an array of shape (n,) containing only zeros.
          >
          A.X - B = O
          min(X)
          >
          thanks.

          Comment

          • faulal@gmail.com

            #6
            Re: solving equation system


            TG wrote:
            Hi there.
            >
            Anyone knows how to use numpy / scipy in order to solve this ?
            >
            * A is an array of shape (n,)
            * X is a positive float number
            * B is an array of shape (n,)
            * O is an array of shape (n,) containing only zeros.
            >
            A.X - B = O
            min(X)
            >
            thanks.

            Comment

            • Cameron Laird

              #7
              Re: solving equation system

              In article <1153141039.216 694.51240@b28g2 000cwb.googlegr oups.com>,
              TG <girodt@gmail.c omwrote:
              >Hi there.
              >
              >Anyone knows how to use numpy / scipy in order to solve this ?
              >
              >* A is an array of shape (n,)
              >* X is a positive float number
              >* B is an array of shape (n,)
              >* O is an array of shape (n,) containing only zeros.
              >
              >A.X - B = O
              >min(X)
              >
              >thanks.
              >
              In general, no X solves the equality, so it's easy to minimize X.

              If A is a non-zero positive multiple of B, exactly one X solves
              the equality, so it's again easy to minimize.

              If A and B vanish, choose X as the smallest positive float.

              I suspect you intended a slightly different problem. It's hard
              to guess what it is.

              Comment

              • Ben C

                #8
                Re: solving equation system

                On 2006-07-17, TG <girodt@gmail.c omwrote:
                >
                Ben C wrote:
                >On 2006-07-17, TG <girodt@gmail.c omwrote:
                Hi there.
                >
                Anyone knows how to use numpy / scipy in order to solve this ?
                >
                * A is an array of shape (n,)
                * X is a positive float number
                * B is an array of shape (n,)
                * O is an array of shape (n,) containing only zeros.
                >
                A.X - B = O
                min(X)
                >>
                >Are we solving for A, B or X? And what do you mean by min(X)?
                >>
                >If we're solving for X there will be many combinations of A and B for
                >which there is no solution.
                >
                Sorry for the poor explanation. I'm trying to put it clear now.
                >
                i've got A and B. I'm looking for X. I made a mistake in my equation
                >:-/
                >
                It's more like :
                >
                A.X - B >= O
                How about this:

                from random import *

                def solve(A, B):
                return reduce(max, (float(b) / a for a, b in zip(A, B)))

                def test():
                A = [random() for i in range(4)]
                B = [random() for i in range(4)]

                x = solve(A, B)

                for a, b in zip(A, B):
                print a, b, a * x - b

                test()

                This only works if all elements of both A and B are positive.
                Well, maybe it will be much more simple if I explain the underlying
                problem :
                >
                I have an array of N dimensions (generally 2).
                - A first calculation gives me a set of integer coordinates inside this
                array, which I will call the point W.
                Is this an array of points, or an array of values, that contains only
                one point?
                - After several other calculations, I've got a set of coordinates in
                this N dimensional space that are floating values, and not bound to the
                limits of my original N-array. This is the point L.
                >
                What I want to do is to translate the point L along the vector LW
                Do you mean the vector L - W? (LW is a scalar, assuming dot product).
                in order to get a point L' which coordinates are inside the original
                N-dimensional array. Then it will be easy to get the closest integer
                coordinates from L'.
                I'm not sure this is clear ... pretty hard to talk about maths in
                english.
                Not very clear to me I'm afraid!

                Comment

                • Carl Banks

                  #9
                  Re: solving equation system

                  TG wrote:
                  Hi there.
                  >
                  Anyone knows how to use numpy / scipy in order to solve this ?
                  >
                  * A is an array of shape (n,)
                  * X is a positive float number
                  * B is an array of shape (n,)
                  * O is an array of shape (n,) containing only zeros.
                  >
                  A.X - B = O
                  min(X)
                  >
                  thanks.
                  Looks like an incorrectly specified degenerate linear least squares
                  problem. The function numpy.linalg.li near_least_squa res might be able
                  to do what you want.


                  Carl Banks

                  Comment

                  • Carl Banks

                    #10
                    Re: solving equation system

                    TG wrote:
                    Sorry for the poor explanation. I'm trying to put it clear now.
                    >
                    i've got A and B. I'm looking for X. I made a mistake in my equation
                    :-/
                    >
                    It's more like :
                    >
                    A.X - B >= O
                    >
                    Well, maybe it will be much more simple if I explain the underlying
                    problem :
                    >
                    I have an array of N dimensions (generally 2).
                    - A first calculation gives me a set of integer coordinates inside this
                    array, which I will call the point W.
                    - After several other calculations, I've got a set of coordinates in
                    this N dimensional space that are floating values, and not bound to the
                    limits of my original N-array. This is the point L.
                    >
                    What I want to do is to translate the point L along the vector LW in
                    order to get a point L' which coordinates are inside the original
                    N-dimensional array. Then it will be easy to get the closest integer
                    coordinates from L'.
                    I see. You have a simple linear programming problem. These can be
                    tricky in general. Because you only have one variable, it's probably
                    ok to use brute force. Try this:

                    given X, A, and B:

                    E = A*X-B
                    C = numpy.where(E<0 ,B/A,X)
                    X = min(C)

                    This assumes that you've designed the problem such that B/A would be
                    less than X where A*X-B<0 (if the opposite were true then of course
                    you'd need max(C)).


                    Carl Banks

                    Comment

                    Working...