Rolando V. RaqueñoWednesday, October 21, 2015 1 Special Function Implementation in IDL A Case Study.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Chapter Matrices Matrix Arithmetic
Introduction to Matlab
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Angela Des Jardins.
MF-852 Financial Econometrics
Maths for Computer Graphics
Matrix Definition A Matrix is an ordered set of numbers, variables or parameters. An example of a matrix can be represented by: The matrix is an ordered.
Intro to Matrices Don’t be scared….
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Fundamentals of matrices
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
Chapter 2 Systems of Linear Equations and Matrices Section 2.4 Multiplication of Matrices.
Chapter 10 Review: Matrix Algebra
ECON 1150 Matrix Operations Special Matrices
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Multi-Dimensional Arrays
Chapter 5.7 Properties of Matrices. Basic Definitions It is necessary to use capital letters to name matrices. Also, subscript notation is often used.
TH EDITION LIAL HORNSBY SCHNEIDER COLLEGE ALGEBRA.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Lecture 28: Mathematical Insight and Engineering.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
Making friends with IDL IDL is an interpreted rather than a compiled language. This means that large IDL programs can execute less rapidly than equivalent.
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Maria Kazachenko
Working with Arrays in MATLAB
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
CSCI 171 Presentation 9 Matrix Theory. Matrix – Rectangular array –i th row, j th column, i,j element –Square matrix, diagonal –Diagonal matrix –Equality.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matrix Algebra Section 7.2. Review of order of matrices 2 rows, 3 columns Order is determined by: (# of rows) x (# of columns)
3.4 Solution by Matrices. What is a Matrix? matrix A matrix is a rectangular array of numbers.
Unit 3 Matrix Arithmetic IT Disicipline ITD 1111 Discrete Mathematics & Statistics STDTLP 1 Unit 3 Matrix Arithmetic.
The MatLab Language Mathematics Laboratory The MathWorks
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
LINEAR MODELS AND MATRIX ALGEBRA
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Notes Over 4.2 Finding the Product of Two Matrices Find the product. If it is not defined, state the reason. To multiply matrices, the number of columns.
Do Now: Perform the indicated operation. 1.). Algebra II Elements 11.1: Matrix Operations HW: HW: p.590 (16-36 even, 37, 44, 46)
4-3 Matrix Multiplication Objective: To multiply a matrix by a scalar multiple.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Matrices.
MTH108 Business Math I Lecture 20.
13.4 Product of Two Matrices
Linear Algebraic Equations and Matrices
MATRICES.
12-1 Organizing Data Using Matrices
Multiplying Matrices.
WarmUp 2-3 on your calculator or on paper..
Matrices and Matrix Operations
Statistics Function Implementation
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Multiplying Matrices.
Matrix Algebra.
Multiplying Matrices.
Working with Arrays in MATLAB
Multiplying Matrices.
Matrix Multiplication Sec. 4.2
Multiplying Matrices.
Presentation transcript:

Rolando V. RaqueñoWednesday, October 21, Special Function Implementation in IDL A Case Study

Rolando V. RaqueñoWednesday, October 21, Special Functions You have been asked or will be asked to implement to implement certain special functions that is commonly used in imaging and image system analysis Examples –Rect –Sinc –Gaus

Rolando V. RaqueñoWednesday, October 21, Implementation Function definitions are mathematically unambiguous –the implementation of these functions are as varied as writing styles and skill are between two individuals Rect function –definition to implementation a –point out the various forms used in traditional programming languages and IDL

Rolando V. RaqueñoWednesday, October 21, Mathematical Definition of One-Dimensional Rect Function

Rolando V. RaqueñoWednesday, October 21, Plot of One-Dimensional Rect Function

Rolando V. RaqueñoWednesday, October 21, Uses of Rect function Used to describe the profile across an aperture. 2D Rect function can be used as a filter or mask in both the spatial and frequency domain

Rolando V. RaqueñoWednesday, October 21, Common Implementation Pitfall Function construct not used Printing out results from within the Rect function Looping to generate the range of x values is embedded into the Rect function. Use of a three parameter function for the arguments x, x0, and b

Rolando V. RaqueñoWednesday, October 21, Implementation Styles Scalar method –More Generalized and Traditional Style –Applicable to most programming languages Vector method –More Efficient in Vectorized Langauages

Rolando V. RaqueñoWednesday, October 21, Sample Implementation function rect, x, x0, b a = abs( (x-x0)/b ) if (a gt 0.5 ) then return, 0.0 if (a lt 0.5 ) then return, 1.0 if( a eq 0.5 ) then return, 0.5 end

Rolando V. RaqueñoWednesday, October 21, Quick Test of Rect Function IDL> print, rect(0.0,0.0,1.0) IDL> print, rect(0.5,0.0,1.0) IDL> print, rect(1.0,0.0,1.0)

Rolando V. RaqueñoWednesday, October 21, Additional Test of Rect Function IDL> for x=-2.0,2.0,0.5 do print,x,rect(x,0.0,1.0)

Rolando V. RaqueñoWednesday, October 21, Simplifying the Parameter List Through the use of variable substitution, the parameter list can be simplified from Rect(x,x0,b) to Rect((x-x0)/b)

Rolando V. RaqueñoWednesday, October 21, Rect Function Implementation (Single if statement) function rect, x a = abs( x ) if (a gt 0.5 ) then answer = 0.0 $ else if (a lt 0.5 ) then answer = 1.0 $ else answer = 0.5 return, answer end

Rolando V. RaqueñoWednesday, October 21, Rect Function Implementation Blocked if statements function rect, x a = abs( x ) if (a gt 0.5 ) then begin answer = 0.0 endif else if (a lt 0.5 ) then begin answer = 1.0 endif else begin answer = 0.5 endelse return, answer end

Rolando V. RaqueñoWednesday, October 21, Rect Function Test IDL> print,rect(0.0) IDL> print,rect(0.5) IDL> print,rect(1.0)

Rolando V. RaqueñoWednesday, October 21, Additional Rect Function Test IDL> for x=-2.0,2.0,0.5 do print,x,rect((x-x0)/b)

Rolando V. RaqueñoWednesday, October 21, D Rect Definition Use of functions is imperative Other methods very awkward and error prone

Rolando V. RaqueñoWednesday, October 21, Error-prone Implementation function rect2d,x,x0,b,y,y0,d answer=rect(x,x0,b)* $ rect(y,y0,d) return, answer end

Rolando V. RaqueñoWednesday, October 21, Error-prone Interface Too many possibilities of transposing parameter values –Difficult for routine to trap. IDL>print,rect2d(0.0,0.0,1.0,0.0,0.0,1.0) Simplified parameter rect function, the above interface becomes less confusing.

Rolando V. RaqueñoWednesday, October 21, D Rect Function function rect2d, x, y answer = rect(x) * rect(y) return, answer end

Rolando V. RaqueñoWednesday, October 21, Test of 2D Rect Function IDL> print,rect2d(0.0,0.0) IDL> print,rect2d(0.5,0.0) IDL> print,rect2d(0.0,0.5) IDL> print,rect2d(0.5,0.5) IDL> print,rect2d(1.0,1.0)

Rolando V. RaqueñoWednesday, October 21, Program to Test rect2d function pro test_rect2d answer=fltarr(5,5) & j=0 for y=-1.0, 1.0, 0.5 do begin i=0 for x=-1.0, 1.0, 0.5, do begin answer(i,j)=rect2d(x,y) i=i+1 endfor j=j+1 endfor plot,answer end

Rolando V. RaqueñoWednesday, October 21, The Vectorized Approach In IDL, there is a better way of dealing with this problem. But first...

Rolando V. RaqueñoWednesday, October 21, Advanced Array Processing in IDL

Rolando V. RaqueñoWednesday, October 21, Accesing Array Values IDL> a = [0,2,4,1] IDL> b = [6,5,1,8,4,3] IDL> print, b(a) Use the values of array a as indices to “look up” or “point to” the values of array b

Rolando V. RaqueñoWednesday, October 21, Simple Image Example IDL> image=bindgen(5,5) IDL> print,image

Rolando V. RaqueñoWednesday, October 21, Subsample Image using for loop pro subsample_loop a=bindgen(5,5) & answer=bytarr(3,3) i=0 for m=0,4,2 do begin j=0 for n=0,4,2 do begin answer(i,j)=a(m,n) j=j+1 endfor i=i+1 endfor print,answer end

Rolando V. RaqueñoWednesday, October 21, Result of subsample_loop IDL> subsample_loop

Rolando V. RaqueñoWednesday, October 21, Analyzing the pattern of indices a[0,0]a[2,0]a[4,0] a[0,2]a[2,2]a[4,2] a[0,4]a[2,4]a[4,4]

Rolando V. RaqueñoWednesday, October 21, Looking at the row indices a[0,0]a[2,0]a[4,0] a[0,2]a[2,2]a[4,2] a[0,4]a[2,4]a[4,4]

Rolando V. RaqueñoWednesday, October 21, Looking at the column indices a[0,0]a[2,0]a[4,0] a[0,2]a[2,2]a[4,2] a[0,4]a[2,4]a[4,4]

Rolando V. RaqueñoWednesday, October 21, Defining a new variable row IDL> row=[[0,2,4],[0,2,4],[0,2,4]] IDL> print,row 0 2 4

Rolando V. RaqueñoWednesday, October 21, Defining a new variable column IDL> column=[[0,0,0],[2,2,2],[4,4,4]] IDL> print,column

Rolando V. RaqueñoWednesday, October 21, IDL can use arrays/matrices as indices into other arrays IDL> b=a[row,column] IDL> print,b

Rolando V. RaqueñoWednesday, October 21, Creating the row and column index matrices Currently, we created the row and column index matrices by hand. We could use a for loop to fill the values, but there is a more convenient way in IDL using the # operator

Rolando V. RaqueñoWednesday, October 21, The # operator For a given array a and b, a # b will have the effect of multiplying the columns of the first array by the rows of the second array Suppose a = [0,2,4] and b = [1,1,1] then IDL> a=[0,2,4] IDL> b=[1,1,1] IDL> print,a#b 0 2 4

Rolando V. RaqueñoWednesday, October 21, The # operator Likewise IDL> print,b#a

Rolando V. RaqueñoWednesday, October 21, The General Form of Creating Index Matrices For the Row Index Matrix ( rim ) given a vector row containing the indices for a given row IDL> rim=row#replicate(1,n_elements(column)) Where replicate(1,n_elements(column)) is creating a column of ones (1) equal to the number of elements in the vector column

Rolando V. RaqueñoWednesday, October 21, The General Form of Creating Index Matrices For the Column Index Matrix ( cim ) given a vector column containing the indices for a given column IDL> cim=replicate(1,n_elements(row))#columns Where replicate(1,n_elements(row)) is creating a row of ones (1) equal to the number of elements in the vector row

Rolando V. RaqueñoWednesday, October 21, Vectorized rect function function rect, x answer = x abs_x = abs(x) gt_half = where( abs_x gt 0.5, count_gt) eq_half = where( abs_x eq 0.5, count_eq ) lt_half = where( abs_x lt 0.5, count_lt ) if (count_gt ne 0) then answer(gt_half)=0.0 if (count_eq ne 0) then answer(eq_half)=0.5 if (count_lt ne 0) then answer(lt_half)=1.0 return, answer end

Rolando V. RaqueñoWednesday, October 21, Sample Use of Vectorized rect IDL> x=[-1.0,-0.5,0.0,0.5,1.0] OR IDL> x=findgen(5)* IDL> print,rect(x)

Rolando V. RaqueñoWednesday, October 21, What about Vectorized rect2d function? We don’t need to do anything!

Rolando V. RaqueñoWednesday, October 21, Testing Procedure of the Vectorized rect2d Define the extents of the x and y values you want to calculate Create the x and y index matrix (or more appropriately x and y value matrix) Pass the x and y value matrix into the function

Rolando V. RaqueñoWednesday, October 21, Define the extents of the x and y values IDL> x=[-1.0,-0.5,0.0,0.5,1.0] IDL> y=[-2.0,-1.5,-1.0, -0.5,0.0,0.5,1.0,1.5,2.0]

Rolando V. RaqueñoWednesday, October 21, Create the x and y value matrices IDL> xm=x#replicate(1.0,n_elements(y)) IDL> print,xm

Rolando V. RaqueñoWednesday, October 21, Create the x and y value matrices IDL> ym=replicate(1.0,n_elements(x))#y IDL> print,ym

Rolando V. RaqueñoWednesday, October 21, Pass the x and y value matrix into the function IDL> print,rect2d(xm,ym)

Rolando V. RaqueñoWednesday, October 21, Statistics Function Implementation (Revisited) Vector Programming Approach Using Matrix Multiplication

Rolando V. RaqueñoWednesday, October 21, sum.pro function sum, x n = n_elements( x ) answer = 0.0 for i = 0L, n-1 do begin answer = answer + x(i) endfor return, answer end

Rolando V. RaqueñoWednesday, October 21, sum.pro function sum, x n = n_elements( x ) a = reform( x, n) answer=a##transpose(replicate(1.0,n)) return, answer end

Rolando V. RaqueñoWednesday, October 21, Using Matrix Multiplication reform ##= transpose(replicate(1.0,n))

Rolando V. RaqueñoWednesday, October 21, sum_squares.pro function sum_squares, x squares = x^2.0 answer= sum( squares ) return, answer end

Rolando V. RaqueñoWednesday, October 21, sum_squares.pro function sum_squares, x n = n_elements( x ) a = reform( x, n) answer = a ## transpose(a) return, answer end

Rolando V. RaqueñoWednesday, October 21, Using Matrix Multiplication reform ##=