正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 9.1.3 When manipulating complex numbers, I can take advantage ofManipulating complex numbers the fact that some things are easy to do in Cartesian(or real and magnitude Complex number has maginary)coordinates, and some things are easy to do in polar (or magnitude and angle)coordinates. For example, adding two m TRAs o eal, imag. mag, angle complex numbers is most easily conceptualized in Cartesian (define (+e z1 22) coordinates, while multiplying two complex numbers is most Eron-rect ( (real z1)(real easily conceptualized in polar coordinates. Addition is in fact just vector addition, so I use the selectors to get the parts of each (define(+o a 2) complex number, add them together using standard addition, make-comp1ex-Ex。np。1ax《 (nag 32)) (angle z2)))) Here i will need to use a constructor that is making a compler a then glue the two parts together to make a new complex number 6001 SICP number given Cartesian coordinates as input. For multiplication, I will use the polar selectors to get out the parts, so that I can just use normal multiplication on the magnitudes, and normal addition on the angles to get the new components. Here, I will use a constructor to make a new complex number that knows its inputs are provided in p of a data abstraction from its impementation. I can write code that lar coordinates Note once more how we are separating the use uses complex numbers, while assuming that someone else will eventually provide a specific implementation. And note the standard form of such procedures: we use selectors to get out the parts, apply more primitive operations to those parts, then reglue the parts back into a data abstraction Berts data structure Slide 9. 1. 4 While it is convenient to separate data abstraction implementation from data abstraction use, ultimately we will have to provide a detailed implementation, that is, we will need to build the "guts " of this abstraction Suppose we ask our friend Bert to provide an implementation for complex numbers 6001 SICP Slide 9.1.5 Bert's data structure First, Bert will need a way of gluing things together. He decides (define(make-complex-from-rect rl im)(list ni im)) rather naturally, just to use lists to glue pieces together. He still (define(make-complex-from-polar mg an) has a choice, though, and being a rather "square"guy, Bert (ist (mg(cos an)) simply opts to use rectangular coordinates as his basis. That means that Bert's representation of a complex number is as a list of a real and imaginary part Note what this means. If we hand bert a real and imaginary part for a new complex number, he can simply glue them together using list as this directly meets his representation. On the other hand, if we hand Bert a magnitude and angle, he will needd 6001联0 to convert these to real and imaginary equivalents(using the these numbers. Thus, Bert's internal representation is always as a list of real and imaginary part ow he represents appropriate trigonometry)so that he can then make a list of the real and imaginary part, which is6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 9.1.3 When manipulating complex numbers, I can take advantage of the fact that some things are easy to do in Cartesian (or real and imaginary) coordinates, and some things are easy to do in polar (or magnitude and angle) coordinates. For example, adding two complex numbers is most easily conceptualized in Cartesian coordinates, while multiplying two complex numbers is most easily conceptualized in polar coordinates. Addition is in fact just vector addition, so I use the selectors to get the parts of each complex number, add them together using standard addition, then glue the two parts together to make a new complex number. Here I will need to use a constructor that is making a complex number given Cartesian coordinates as input. For multiplication, I will use the polar selectors to get out the parts, so that I can just use normal multiplication on the magnitudes, and normal addition on the angles to get the new components. Here, I will use a constructor to make a new complex number that knows its inputs are provided in polar coordinates. Note once more how we are separating the use of a data abstraction from its implementation. I can write code that uses complex numbers, while assuming that someone else will eventually provide a specific implementation. And note the standard form of such procedures: we use selectors to get out the parts, apply more primitive operations to those parts, then reglue the parts back into a data abstraction. Slide 9.1.4 While it is convenient to separate data abstraction implementation from data abstraction use, ultimately we will have to provide a detailed implementation, that is, we will need to build the "guts" of this abstraction. Suppose we ask our friend Bert to provide an implementation for complex numbers... Slide 9.1.5 First, Bert will need a way of gluing things together. He decides, rather naturally, just to use lists to glue pieces together. He still has a choice, though, and being a rather "square " guy, Bert simply opts to use rectangular coordinates as his basis. That means that Bert's representation of a complex number is as a list of a real and imaginary part. Note what this means. If we hand Bert a real and imaginary part for a new complex number, he can simply glue them together using list as this directly meets his representation. On the other hand, if we hand Bert a magnitude and angle, he will need to convert these to real and imaginary equivalents (using the appropriate trigonometry) so that he can then make a list of the real and imaginary part, which is how he represents these numbers. Thus, Bert's internal representation is always as a list of real and imaginary parts
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有