🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.python
9 messages
9 total messages Started by jcubic Fri, 25 Jun 2010 18:51
Interpolacja Liniowa
#27541
Author: jcubic
Date: Fri, 25 Jun 2010 18:51
22 lines
1860 bytes
Próbuje napisać funkcję do interpolacji liniowej, taką która przyjmuje
cztery parametry x1, y1, x2, y2 i zwracającą funkcję f(x).

Próbowałem taki kod

def get_interpolated(x1, y1, x2, y2):
    'function for linear interpolation.'
    a = (y2 - y1) / (x2 - x1)
    b = ((-x1) * (y2 - y1) - (x2 - x1) * (-y1)) / (x2 - x1)
    def interpolated(x):
        return a * x + b
    return interpolated

i taki

def get_interpolated(x1, y1, x2, y2):
    'function for linear interpolation.'
    def interpolated(x):
        return y1 + (((y2 - y1) / (x2 - x1)) * (x - x1))
    return interpolated

ale żaden nie działa.
Re: Interpolacja Liniowa
#27542
Author: Piotr Chamera
Date: Fri, 25 Jun 2010 21:04
32 lines
1678 bytes
W dniu 2010-06-25 20:51, jcubic pisze:
> Próbuje napisać funkcję do interpolacji liniowej, taką która przyjmuje
> cztery parametry x1, y1, x2, y2 i zwracającą funkcję f(x).
>
> Próbowałem taki kod
>
> def get_interpolated(x1, y1, x2, y2):
>      'function for linear interpolation.'
>      a = (y2 - y1) / (x2 - x1)
>      b = ((-x1) * (y2 - y1) - (x2 - x1) * (-y1)) / (x2 - x1)
>      def interpolated(x):
>          return a * x + b
>      return interpolated

ale co nie działa?

 >>> f = get_interpolated(0, 0, 1, 1)
 >>> f
<function interpolated at 0x00C57DB0>
 >>> f(2)
2
 >>> f(3)
3

 >>> g = get_interpolated(0, 0, 1, 2)
 >>> g(2)
4
 >>> g(-23)
-46

 >>> get_interpolated(0, 1, 1, 2)(10)
11
Re: Interpolacja Liniowa
#27543
Author: rav
Date: Sat, 26 Jun 2010 18:41
12 lines
1637 bytes
Nie dziala kiedy x2-x1 == 0 (div by zero)
dlatego dodalbym sprawdzenie roznicy tych wartosci do funkcji

def getInterpolated(x0,y0, x1, y1):
    if x1 - x0 == 0:
        print 'x1 i x0 can\'t be equal'
        return None
    a = (y1 - y0) / (x1 - x0)
    b = y0 - a * x0
    def interpolate(x):
        return a * x + b
    return None
Re: Interpolacja Liniowa
#27547
Author: jcubic
Date: Sun, 27 Jun 2010 09:33
14 lines
1449 bytes
Pierwszy przypadek:

>> f = get_interpolated(1, 1, 1000, 200)
>> f(100)
0

Drugi przypadek
>> f = get_interpolated(1,1, 1000, 200)
>> f(100)
1
>> f(200)
1
>> f(300)
1
Re: Interpolacja Liniowa
#27548
Author: Piotr Chamera
Date: Sun, 27 Jun 2010 11:14
29 lines
1475 bytes
W dniu 2010-06-27 11:33, jcubic pisze:
> Pierwszy przypadek:
>
>>> f = get_interpolated(1, 1, 1000, 200)
>>> f(100)
> 0
>
> Drugi przypadek
>>> f = get_interpolated(1,1, 1000, 200)
>>> f(100)
> 1
>>> f(200)
> 1
>>> f(300)
> 1

Porównaj

 >>> f = get_interpolated(1, 1, 1000, 200)
 >>> f (100)
0

z tym

 >>> g = get_interpolated(1.0, 1.0, 1000.0, 200.0)
 >>> g(100.0)
20.72072072072072

i wyciągnij wnioski :)
Re: Interpolacja Liniowa
#27544
Author: Bart Ogryczak
Date: Mon, 28 Jun 2010 16:49
25 lines
2062 bytes
On Jun 26, 8:41 pm, rav <rafalgulin...@gmail.com> wrote:
> Nie dziala kiedy x2-x1 == 0 (div by zero)
> dlatego dodalbym sprawdzenie roznicy tych wartosci do funkcji
>
> def getInterpolated(x0,y0, x1, y1):
>     if x1 - x0 == 0:
>         print 'x1 i x0 can\'t be equal'
>         return None
>     a = (y1 - y0) / (x1 - x0)
>     b = y0 - a * x0
>     def interpolate(x):
>         return a * x + b
>     return None

Pythonistycznie, to raczej będzie tak:

try:
    a = (y2 - y1) / (x2 - x1)
    b = ((-x1) * (y2 - y1) - (x2 - x1) * (-y1)) / (x2 - x1)
    def interpolate(x):
        return a * x + b
    return interpolate
except ZeroDivisionError:
    print 'x1 i x0 can\'t be equal'
    return None
Re: Interpolacja Liniowa
#27545
Author: rav
Date: Tue, 29 Jun 2010 09:08
34 lines
2608 bytes
On 28 Cze, 18:49, Bart Ogryczak <b.ogryc...@gmail.com> wrote:
> On Jun 26, 8:41 pm, rav <rafalgulin...@gmail.com> wrote:
>
> > Nie dziala kiedy x2-x1 == 0 (div by zero)
> > dlatego dodalbym sprawdzenie roznicy tych wartosci do funkcji
>
> > def getInterpolated(x0,y0, x1, y1):
> >     if x1 - x0 == 0:
> >         print 'x1 i x0 can\'t be equal'
> >         return None
> >     a = (y1 - y0) / (x1 - x0)
> >     b = y0 - a * x0
> >     def interpolate(x):
> >         return a * x + b
> >     return None
>
> Pythonistycznie, to raczej będzie tak:
>
> try:
>     a = (y2 - y1) / (x2 - x1)
>     b = ((-x1) * (y2 - y1) - (x2 - x1) * (-y1)) / (x2 - x1)
>     def interpolate(x):
>         return a * x + b
>     return interpolate
> except ZeroDivisionError:
>     print 'x1 i x0 can\'t be equal'
>     return None

Przy czestym x2-x1 == 0 "if" bedzie mniej kosztowny ale jezeli ten
warunek bedzie rzadko spelniony to faktycznie wyjatek jest lepszym
rozwiazaniem.

zrodelko:
http://stackoverflow.com/questions/2522005/cost-of-exception-handlers-in-python
Re: Interpolacja Liniowa
#27546
Author: Bart Ogryczak
Date: Tue, 29 Jun 2010 09:39
43 lines
2856 bytes
On Jun 29, 11:08 am, rav <rafalgulin...@gmail.com> wrote:
> On 28 Cze, 18:49, Bart Ogryczak <b.ogryc...@gmail.com> wrote:
>
>
>
>
>
> > On Jun 26, 8:41 pm, rav <rafalgulin...@gmail.com> wrote:
>
> > > Nie dziala kiedy x2-x1 == 0 (div by zero)
> > > dlatego dodalbym sprawdzenie roznicy tych wartosci do funkcji
>
> > > def getInterpolated(x0,y0, x1, y1):
> > >     if x1 - x0 == 0:
> > >         print 'x1 i x0 can\'t be equal'
> > >         return None
> > >     a = (y1 - y0) / (x1 - x0)
> > >     b = y0 - a * x0
> > >     def interpolate(x):
> > >         return a * x + b
> > >     return None
>
> > Pythonistycznie, to raczej będzie tak:
>
> > try:
> >     a = (y2 - y1) / (x2 - x1)
> >     b = ((-x1) * (y2 - y1) - (x2 - x1) * (-y1)) / (x2 - x1)
> >     def interpolate(x):
> >         return a * x + b
> >     return interpolate
> > except ZeroDivisionError:
> >     print 'x1 i x0 can\'t be equal'
> >     return None
>
> Przy czestym x2-x1 == 0 "if" bedzie mniej kosztowny ale jezeli ten
> warunek bedzie rzadko spelniony to faktycznie wyjatek jest lepszym
> rozwiazaniem.
>
> zrodelko:http://stackoverflow.com/questions/2522005/cost-of-exception-handlers...

No, jeśli chodzi o wydajność, to się zgadza. Natomiast jeśli chodzi o
styl to
http://stackoverflow.com/questions/1835756/
Re: Interpolacja Liniowa
#27549
Author: mrk
Date: Tue, 29 Jun 2010 10:24
19 lines
1934 bytes
On 25 Cze, 20:51, jcubic <jcu...@onet.pl> wrote:
> Próbuje napisać funkcję do interpolacji liniowej, taką która przyjmuje
> cztery parametry x1, y1, x2, y2 i zwracającą funkcję f(x).

Lepiej chyba by było zastosować opis parametryczny, czyli (x,y)=f(k),
interpolacja by wyglądała wtedy tak (nie ma problemu z x1==x2)

def get_interpolated(x1, y1, x2, y2):
  def interpolated(k):
    return x1+(x2-x1)*k, y1+(y2-y1)*k
  return interpolated

>>>f = get_interpolated(0.0, 0.0, 10.0, 5.0)
>>>print f(0.0)
(0.0, 0.0)
>>>print f(1.0)
(10.0, 5.0)
>>>print f(0.5)
(5, 2.5)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads