Abstraction Barriers : Boundaries Giving Base To Data Abstraction

Abstraction Barriers

You can read about data abstraction in my previous post ,in this blog I’ll talk about the abstraction barriers.When we represent a rational numbers in terms of pair with operations like make-rat and selectors like numer and denom .Basic idea behind data abstraction is to identify for each type of data object a basic set of operations in terms of which all manipulations of data object can be expressed and then using those procedures to manipulate the data.

abstraction_barriers-c18940be

In above figure as you can see that we have levels of abstraction which fulfills different needs. But these levels are divided by horizontal boundaries which are known as Abstraction barriers. Different levels are isolated from each other. At each level , the barrier seperates the programs(above) that use the data abstraction from the program below, that implement data abstraction. As we can see from figure that we started from root level i.e primitive level and regularly abstracting different levels which doesn’t care about how below level is implemented. Programs at the top level only uses public procedure like add-rat , sub-rat ,mul-rat etc. These procedures are implemented in terms of make-rat ,numer,denom etc which are again implemented in terms of pairs.We can say that procedures at each level are interfaces that define abstraction barriers and connect the different levels.
So creating abstraction barriers is a simple idea, but the biggest advantage of it – makes program much easier to maintain and modify.But one disadvantage is the representation of data/procedures influences the program that operate on it, therefore slight change in representation can modify corresponding procedure accordingly. So this change could be expensive and time consuming if there is large program and this will depend on what level change is happening like if the change is at ground level it will be more expensive and time consuming compare to change at higher level. So we have to confine design to fewer program modules and try to make it less dependent on representation.
So abstraction barrier is one of the steps to do data abstraction in your program.

Finding Roots By Half Interval Method

Fnding Roots by half-interval- method

The half interval method is the most basic method to find roots of an equation i.e to find x such  that f(x) = 0 , where f is a continuos function. Now the idea is if we have two points a and such that f(a) < 0 < f(b) then f must have  atleast one zero between  a and b. So to locate a zero we’ll take average of  a and b and compute f(x) where x is average of a and b.If f(x) > 0,then f must have zero between a and x.If f(x) < 0 , then f must have zero between x and b.So we continue this way to identify smaller and smaller intervals on which f must have  a zero.Therefore when we reach a point where interval is small enough the process stops. Since we are calculating average at each level and decreasing our level of uncertainity by half , the number of steps required grows as O (log (L/T)) ,where L is the length of original interval i.e difference of a and b and T is the error tolereance (size of interval we consider small enough).

(define (search f neg-point pos-point)
  (let ((midpoint (average neg-point pos-point)))
    (if (close-enough? neg-point pos-point)
        midpoint
        (let ((test-value (f midpoint)))
          (cond ((positive? test-value)
                  (search f neg-point midpoint))
                ((negative? test-value)
                 (search f midpoint pos-point))
                (else midpoint))))))

Above pseudo-code is taken from Structure And Interpretation of Computer Programs.Now the close-enough? used above which is breaking condition for the recursive function will be :

(define (close-enough? x y)
  (< (abs (- x y)) 0.001))

close-enough? function will decide how much accurate will be our answer.

To summarise we are getting roots of the equation in O(log (L/T)) by half-interval method.

WHY Data Abstraction ??

Learn what is data abstraction,why we do it, what will be advantage of data abstraction

What’s an Abstraction?

Abstraction is a technique for managing complexity of computer systems. So programmer implements abstraction by dividing the complex problems into sub-problems i.e creating fine line between between sub-problems which will be more easy to implement,less complex for the programmer to implement and help in making a system less prone to error.So due to abstraction a team can be more efficient and can solve more complex problems.It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level.So if we write a code for adding  numbers we don’t have to worry about  how numbers are represented and how addition is happening at lower level, so those details have been suppressed ,it can be said that they were abstracted away, leaving simply numbers and addition with which we can work upon.

Abstraction can be done in two ways :

1- Procedural Abstraction

2- Data Abstraction

Continue reading “WHY Data Abstraction ??”