Multimethods for abstraction and
performance
Jiahao Chen
Research Scientist
MIT CSAIL
jiahao.github.io
GitHub: @jiahao
People like to reinvent programming languages
Alan Edelman Andreas Noack Xianyi Zhang Jarrett Revels
Oscar BlumbergDavid Sanders
The
Julia Lab
at MIT
Simon Danisch
Jiahao Chen
Weijian Zhang
U. Manchester
Jake Bolewski
USAP
Shashi GowdaAmit Murthy Tanmay Mohapatra
Eka Palamadai
Collaborators
Joey Huchette
Isaac Virshup
Steven Johnson
MIT Mathematics
Yes Sian Ng Miles Lubin Iain Dunning
Jon Malmaud Simon Kornblith
Yichao Yu
Harvard
Jeremy Kepner
Lincoln Labs
Stavros
Papadopoulos
Intel Labs
Nikos
Patsopoulos
Brigham Woman’s
Hospital
Pete Szolovits
CSAIL
Alex Townsend
MIT Mathematics
Jack Poulson
Stanford
Mike Innes
Julia Computing
Summer of
Code alums
Keno Fischer
Julia Computing
Jameson Nash
Julia Computing
Simon Danisch
Julia Lab
Shashi Gowda
Julia Lab
Leah Hanson
Stripe
John Myles White
Facebook
Jarrett Revels
Julia Lab
2013
2014
2015
Jacob Quinn
Domo
Kenta Sato
U. Tokyo
Rohit Varkey Thankachan
Nat’l Inst. Tech. Karnataka
Simon Danish
Julia Lab
David Gold
U. Washington
Keno FischerJameson NashStefan KarpinskiJeff Bezanson Viral B. Shah
Alums at
Mike Innes
445 contributors to Julia
726 package authors
as of 2016-03-15
https://github.com/jiahao/ijulia-notebooks
The world of
808 packages, 726 authors
445 contributors to julia repo
6,841 stargazers
549 watchers
What’s the big deal about Julia ?
A high level language
with C-like speed
julialang.org/benchmarks
What’s the big deal about Julia ?
Normalized average lines of code (shortest = 0, longest = 1)
Execution time (C = 1)
https://groups.google.com/d/msg/julia-users/BYRAeQJuvTw/O7VK7-vp1EEJ
fast and expressive
A simple example
It bridges the divide between computer science
and computational science
What’s the big deal about Julia ?
It bridges the divide between computer science
and computational science
What’s the big deal about Julia ?
data abstraction
performance
It bridges the divide between computer science
and computational science
What’s the big deal about Julia ?
data abstraction
performance
What if you didn’t have to choose between
data abstraction and performance?
Thesis:
Users want to write generic code.
Users also want native machine performance (when available).
Julia’s generic function system with multimethods (multiple
dispatch) lets you have both.
Is Julia object oriented?
Yes, but maybe not in a way you’re familiar with.*
Generic functions** like in CLOS,
unlike C++ classes.
*G. Castagna, Object Oriented Programming: A Unified Foundation, http://link.springer.com/book/10.1007/978-1-4612-4138-6
**L. G. DeMichiel and R. P. Gabriel, §3.2 of The Common Lisp Object System: An Overview, http://www.dreamsongs.com/Files/ECOOP.pdf
Object-oriented programming with classes
What can I do with/to a thing?
methods objects
Object-oriented programming with classes
What can I do with/to a thing?
methods objects
Object-oriented programming with classes
What can I do with/to a thing?
methods objects
Object-oriented programming with classes
What can I do with/to a thing?
top up
pay fare
lose
buy
methods objects
Object-oriented programming with classes
What can I do with/to a thing?
top up
pay fare
lose
buy
top up
pay fare
lose
buy
methods objects
Object-oriented programming with classes
What can I do with/to a thing?
top up
pay fare
lose
buy
top up
pay fare
lose
buy
methods objects
Object-oriented programming with classes
What can I do with/to a thing?
top up
pay fare
lose
buy
top up
pay fare
lose
buy
pay fare
lose
buy
methods objects
Object-oriented programming with classes
What can I do with/to a thing?
top up
pay fare
lose
buy
classes are more
fundamental
than methods
top up
pay fare
lose
buy
pay fare
lose
buy
OOP with classes multimethods
What can I do with/to a thing?
top up
pay fare
lose
buy
generic
function
objectsmethods
multimethods
relationships between
objects and functions
Multimethods for linear algebra
What can I do with/to a thing?
compute spectral factorization
compute singular values
compute singular values and vectors
compute eigenvalues
generic
function
objectsmethods
Methods can take advantage of special matrix structures
eigvals
eigfact
svdvals
svdfact
Matrix
SymTridiagonal
Bidiagonal
Data types as a lattice
D. Scott, “Data types as lattices”, SIAM J. Comput. 5: 522-87, 1976, doi:10.1137/0205037
Real
Number
FloatingPoint Rational
Complex
Float64 BigFloat…
…
Integer
is a parameter of
is a subtype of
Signed BigInt
Any
Int64…
…
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
no method here
try supertype
super(Int64) = Signed
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
no method here
try supertype
super(Int64) = Signed
no method here either
super(Signed) = Integer
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
no method here
try supertype
super(Int64) = Signed
no method here either
super(Signed) = Integer
found a method
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
Signed
Multimethods with type lattice traversal
Real
Number
FloatingPoint Rational
Float64 BigFloat…
Integer
is a parameter of
is a subtype of
BigInt
Any
Int64…
…
easy to call external C
functions, e.g. CLAPACK
sstev, dstev…
So how does this help us with linear algebra?
Multi-method dispatch with generic fallbacks
Matrix operations on general rings
So how does this help us with linear algebra?
Multi-method dispatch with generic fallbacks
Matrix operations on general rings textbook algorithm
So how does this help us with linear algebra?
Multi-method dispatch with generic fallbacks
Matrix operations on general rings
Native parallelism constructs
Native parallelism constructs
Native parallelism constructs
JuMP: a domain specific language
Iain Dunning Miles Lubin
MIT
Operations Research
Joey HuchetteYes Sian Ng
http://github.com/JuliaLang/IJulia.jl
http://blog.jupyter.org/2016/02/16/jupyterdays-boston-2016/
In summary,
Types helps users express scientific computations
and helps the compiler specialize code for performance
Other advanced features for genericness and performance:
metaprogramming with macros and generated functions,
parametric polymorphism, native parallel computing, …
try Julia today!
juliabox.org
JuliaCon - June 21-25 at MIT CSAIL
juliacon.org
Other references
B. Liskov, The Power of Abstraction, Turing Award lecture,
OOPSLA’09. https://www.youtube.com/watch?
v=qAKrMdUycb8
J. Sammet, Programming Languages: History and
Fundamentals, Prentice-Hall, 1969.
Are classes really that bad?
Yes, when you break their abstraction.
http://okmij.org/ftp/Computation/Subtyping/
The circle-ellipse problem (aka the square-rectangle problem)
The circle-ellipse problem (aka the square-rectangle problem)
Is a circle an ellipse?
The circle-ellipse problem (aka the square-rectangle problem)
Is a circle an ellipse?
Yes: it is a special case of an ellipse with
degenerate foci
zero eccentricity
etc.
The circle-ellipse problem (aka the square-rectangle problem)
Is a circle an ellipse?
Yes: it is a special case of an ellipse with
degenerate foci
zero eccentricity
etc.
No: it makes no sense to change the second focus of a circle.
The circle-ellipse problem
#Program A
foo = Ellipse(r1 = 0.5, r2 = 1.0)
get_r2(foo)
The circle-ellipse problem
#Program A
foo = Ellipse(r1 = 0.5, r2 = 1.0)
get_r2(foo)
Circle(r = 0.5)
The circle-ellipse problem
#Program A
foo = Ellipse(r1 = 0.5, r2 = 1.0)
get_r2(foo)
Circle(r = 0.5)
You can make this program work: define
get_r2(x::Circle) = x.r
#Program B
foo = Ellipse()
set_r1(foo, 0.5)
set_r2(foo, 1.0)
get_r1(foo)*get_r2(foo) == 0.5
The circle-ellipse problem
#Program A
foo = Ellipse(r1 = 0.5, r2 = 1.0)
get_r2(foo)
Circle(r = 0.5)
You can make this program work: define
get_r2(x::Circle) = x.r
#Program B
foo = Ellipse()
set_r1(foo, 0.5)
set_r2(foo, 1.0)
get_r1(foo)*get_r2(foo) == 0.5
The circle-ellipse problem
#Program A
foo = Ellipse(r1 = 0.5, r2 = 1.0)
get_r2(foo)
Circle()
Circle(r = 0.5)
You can make this program work: define
get_r2(x::Circle) = x.r
#Program B
foo = Ellipse()
set_r1(foo, 0.5)
set_r2(foo, 1.0)
get_r1(foo)*get_r2(foo) == 0.5
The circle-ellipse problem
#Program A
foo = Ellipse(r1 = 0.5, r2 = 1.0)
get_r2(foo)
Circle()
Circle(r = 0.5)
You can’t substitute a Circle for an Ellipse in this program.
You can make this program work: define
get_r2(x::Circle) = x.r
#Program B
foo = Ellipse()
set_r1(foo, 0.5)
set_r2(foo, 1.0)
get_r1(foo)*get_r2(foo) == 0.5
The circle-ellipse problem
#Program A
foo = Ellipse(r1 = 0.5, r2 = 1.0)
get_r2(foo)
Circle()
Circle(r = 0.5)
You can’t substitute a Circle for an Ellipse in this program.
You can make this program work: define
get_r2(x::Circle) = x.r
If you can mutate objects (“covariant context”)*, then circles
are not ellipses because of the Liskov substitution
principle** (one of the SOLID principles).
*G. Castagna, Covariance and contravariance : conflict without a cause.
In Object Oriented Programming: A Unified Foundation, http://link.springer.com/book/10.1007/978-1-4612-4138-6
**B. Liskov, Keynote address - data abstraction and hierarchy, OOPSLA '87 Proceedings Addendum, 17-34.
B. Liskov and J. M. Wing. "A Behavioral Notion of Subtyping," ACM Trans. Programming Languages and Systems 16(6):1811-1841, Nov 1994.
n-ary operations
mylist = ListOfNumbers();
mylist.push!(1.0);
n-ary operations
Methods are associated with classes.
mylist = ListOfNumbers();
mylist.push!(1.0);
n-ary operations
Methods are associated with classes.
mylist = ListOfNumbers();
mylist.push!(1.0);
A unary operation on a class can be implemented as a
method of that class.
The abstraction of classes can break down for functions
involving multiple classes.
n-ary operations
Neither. Matrix + DiagonalMatrix needs to access private
fields, thus breaking encapsulation*.
*A loaded term; see D. J. Armstrong, The quarks of object-oriented development, Commun. ACM 49 (2): 123-8, Feb 2006, doi:10.1145/1113034.1113040
n-ary operations
Adding a diagonal matrix and an ordinary (dense) matrix:
is it a method of Matrix or DiagonalMatrix?
Neither. Matrix + DiagonalMatrix needs to access private
fields, thus breaking encapsulation*.
*A loaded term; see D. J. Armstrong, The quarks of object-oriented development, Commun. ACM 49 (2): 123-8, Feb 2006, doi:10.1145/1113034.1113040
n-ary operations
Adding a diagonal matrix and an ordinary (dense) matrix:
is it a method of Matrix or DiagonalMatrix?
class Matrix {
private:
int m, n;
float *data;
// …
friend Matrix operator+(Matrix M, DiagonalMatrix D); //non-member function
};
class DiagonalMatrix{
private:
int n;
//store only the diagonals
float *diagonaldata;
// …
friend Matrix operator+(Matrix M, DiagonalMatrix D); //non-member function
};
Neither. Matrix + DiagonalMatrix needs to access private
fields, thus breaking encapsulation*.
*A loaded term; see D. J. Armstrong, The quarks of object-oriented development, Commun. ACM 49 (2): 123-8, Feb 2006, doi:10.1145/1113034.1113040

Julia: Multimethods for abstraction and performance

  • 1.
    Multimethods for abstractionand performance Jiahao Chen Research Scientist MIT CSAIL jiahao.github.io GitHub: @jiahao
  • 2.
    People like toreinvent programming languages
  • 4.
    Alan Edelman AndreasNoack Xianyi Zhang Jarrett Revels Oscar BlumbergDavid Sanders The Julia Lab at MIT Simon Danisch Jiahao Chen Weijian Zhang U. Manchester Jake Bolewski USAP Shashi GowdaAmit Murthy Tanmay Mohapatra Eka Palamadai Collaborators Joey Huchette Isaac Virshup Steven Johnson MIT Mathematics Yes Sian Ng Miles Lubin Iain Dunning Jon Malmaud Simon Kornblith Yichao Yu Harvard Jeremy Kepner Lincoln Labs Stavros Papadopoulos Intel Labs Nikos Patsopoulos Brigham Woman’s Hospital Pete Szolovits CSAIL Alex Townsend MIT Mathematics Jack Poulson Stanford
  • 5.
    Mike Innes Julia Computing Summerof Code alums Keno Fischer Julia Computing Jameson Nash Julia Computing Simon Danisch Julia Lab Shashi Gowda Julia Lab Leah Hanson Stripe John Myles White Facebook Jarrett Revels Julia Lab 2013 2014 2015 Jacob Quinn Domo Kenta Sato U. Tokyo Rohit Varkey Thankachan Nat’l Inst. Tech. Karnataka Simon Danish Julia Lab David Gold U. Washington Keno FischerJameson NashStefan KarpinskiJeff Bezanson Viral B. Shah Alums at Mike Innes
  • 6.
    445 contributors toJulia 726 package authors as of 2016-03-15
  • 7.
    https://github.com/jiahao/ijulia-notebooks The world of 808packages, 726 authors 445 contributors to julia repo 6,841 stargazers 549 watchers
  • 8.
    What’s the bigdeal about Julia ? A high level language with C-like speed julialang.org/benchmarks
  • 9.
    What’s the bigdeal about Julia ? Normalized average lines of code (shortest = 0, longest = 1) Execution time (C = 1) https://groups.google.com/d/msg/julia-users/BYRAeQJuvTw/O7VK7-vp1EEJ fast and expressive
  • 10.
  • 11.
    It bridges thedivide between computer science and computational science What’s the big deal about Julia ?
  • 12.
    It bridges thedivide between computer science and computational science What’s the big deal about Julia ? data abstraction performance
  • 13.
    It bridges thedivide between computer science and computational science What’s the big deal about Julia ? data abstraction performance What if you didn’t have to choose between data abstraction and performance?
  • 14.
    Thesis: Users want towrite generic code. Users also want native machine performance (when available). Julia’s generic function system with multimethods (multiple dispatch) lets you have both.
  • 15.
    Is Julia objectoriented? Yes, but maybe not in a way you’re familiar with.* Generic functions** like in CLOS, unlike C++ classes. *G. Castagna, Object Oriented Programming: A Unified Foundation, http://link.springer.com/book/10.1007/978-1-4612-4138-6 **L. G. DeMichiel and R. P. Gabriel, §3.2 of The Common Lisp Object System: An Overview, http://www.dreamsongs.com/Files/ECOOP.pdf
  • 16.
    Object-oriented programming withclasses What can I do with/to a thing?
  • 17.
    methods objects Object-oriented programmingwith classes What can I do with/to a thing?
  • 18.
    methods objects Object-oriented programmingwith classes What can I do with/to a thing?
  • 19.
    methods objects Object-oriented programmingwith classes What can I do with/to a thing? top up pay fare lose buy
  • 20.
    methods objects Object-oriented programmingwith classes What can I do with/to a thing? top up pay fare lose buy top up pay fare lose buy
  • 21.
    methods objects Object-oriented programmingwith classes What can I do with/to a thing? top up pay fare lose buy top up pay fare lose buy
  • 22.
    methods objects Object-oriented programmingwith classes What can I do with/to a thing? top up pay fare lose buy top up pay fare lose buy pay fare lose buy
  • 23.
    methods objects Object-oriented programmingwith classes What can I do with/to a thing? top up pay fare lose buy classes are more fundamental than methods top up pay fare lose buy pay fare lose buy
  • 24.
    OOP with classesmultimethods What can I do with/to a thing? top up pay fare lose buy generic function objectsmethods multimethods relationships between objects and functions
  • 25.
    Multimethods for linearalgebra What can I do with/to a thing? compute spectral factorization compute singular values compute singular values and vectors compute eigenvalues generic function objectsmethods Methods can take advantage of special matrix structures eigvals eigfact svdvals svdfact Matrix SymTridiagonal Bidiagonal
  • 26.
    Data types asa lattice D. Scott, “Data types as lattices”, SIAM J. Comput. 5: 522-87, 1976, doi:10.1137/0205037 Real Number FloatingPoint Rational Complex Float64 BigFloat… … Integer is a parameter of is a subtype of Signed BigInt Any Int64… …
  • 27.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… …
  • 28.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… …
  • 29.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… …
  • 30.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… … no method here try supertype super(Int64) = Signed
  • 31.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… … no method here try supertype super(Int64) = Signed no method here either super(Signed) = Integer
  • 32.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… … no method here try supertype super(Int64) = Signed no method here either super(Signed) = Integer found a method
  • 33.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… …
  • 34.
    Signed Multimethods with typelattice traversal Real Number FloatingPoint Rational Float64 BigFloat… Integer is a parameter of is a subtype of BigInt Any Int64… …
  • 42.
    easy to callexternal C functions, e.g. CLAPACK sstev, dstev…
  • 43.
    So how doesthis help us with linear algebra? Multi-method dispatch with generic fallbacks Matrix operations on general rings
  • 44.
    So how doesthis help us with linear algebra? Multi-method dispatch with generic fallbacks Matrix operations on general rings textbook algorithm
  • 45.
    So how doesthis help us with linear algebra? Multi-method dispatch with generic fallbacks Matrix operations on general rings
  • 46.
  • 47.
  • 48.
  • 49.
    JuMP: a domainspecific language Iain Dunning Miles Lubin MIT Operations Research Joey HuchetteYes Sian Ng
  • 50.
  • 52.
    In summary, Types helpsusers express scientific computations and helps the compiler specialize code for performance Other advanced features for genericness and performance: metaprogramming with macros and generated functions, parametric polymorphism, native parallel computing, … try Julia today! juliabox.org JuliaCon - June 21-25 at MIT CSAIL juliacon.org
  • 53.
    Other references B. Liskov,The Power of Abstraction, Turing Award lecture, OOPSLA’09. https://www.youtube.com/watch? v=qAKrMdUycb8 J. Sammet, Programming Languages: History and Fundamentals, Prentice-Hall, 1969.
  • 54.
    Are classes reallythat bad? Yes, when you break their abstraction. http://okmij.org/ftp/Computation/Subtyping/
  • 55.
    The circle-ellipse problem(aka the square-rectangle problem)
  • 56.
    The circle-ellipse problem(aka the square-rectangle problem) Is a circle an ellipse?
  • 57.
    The circle-ellipse problem(aka the square-rectangle problem) Is a circle an ellipse? Yes: it is a special case of an ellipse with degenerate foci zero eccentricity etc.
  • 58.
    The circle-ellipse problem(aka the square-rectangle problem) Is a circle an ellipse? Yes: it is a special case of an ellipse with degenerate foci zero eccentricity etc. No: it makes no sense to change the second focus of a circle.
  • 59.
    The circle-ellipse problem #ProgramA foo = Ellipse(r1 = 0.5, r2 = 1.0) get_r2(foo)
  • 60.
    The circle-ellipse problem #ProgramA foo = Ellipse(r1 = 0.5, r2 = 1.0) get_r2(foo) Circle(r = 0.5)
  • 61.
    The circle-ellipse problem #ProgramA foo = Ellipse(r1 = 0.5, r2 = 1.0) get_r2(foo) Circle(r = 0.5) You can make this program work: define get_r2(x::Circle) = x.r
  • 62.
    #Program B foo =Ellipse() set_r1(foo, 0.5) set_r2(foo, 1.0) get_r1(foo)*get_r2(foo) == 0.5 The circle-ellipse problem #Program A foo = Ellipse(r1 = 0.5, r2 = 1.0) get_r2(foo) Circle(r = 0.5) You can make this program work: define get_r2(x::Circle) = x.r
  • 63.
    #Program B foo =Ellipse() set_r1(foo, 0.5) set_r2(foo, 1.0) get_r1(foo)*get_r2(foo) == 0.5 The circle-ellipse problem #Program A foo = Ellipse(r1 = 0.5, r2 = 1.0) get_r2(foo) Circle() Circle(r = 0.5) You can make this program work: define get_r2(x::Circle) = x.r
  • 64.
    #Program B foo =Ellipse() set_r1(foo, 0.5) set_r2(foo, 1.0) get_r1(foo)*get_r2(foo) == 0.5 The circle-ellipse problem #Program A foo = Ellipse(r1 = 0.5, r2 = 1.0) get_r2(foo) Circle() Circle(r = 0.5) You can’t substitute a Circle for an Ellipse in this program. You can make this program work: define get_r2(x::Circle) = x.r
  • 65.
    #Program B foo =Ellipse() set_r1(foo, 0.5) set_r2(foo, 1.0) get_r1(foo)*get_r2(foo) == 0.5 The circle-ellipse problem #Program A foo = Ellipse(r1 = 0.5, r2 = 1.0) get_r2(foo) Circle() Circle(r = 0.5) You can’t substitute a Circle for an Ellipse in this program. You can make this program work: define get_r2(x::Circle) = x.r If you can mutate objects (“covariant context”)*, then circles are not ellipses because of the Liskov substitution principle** (one of the SOLID principles). *G. Castagna, Covariance and contravariance : conflict without a cause. In Object Oriented Programming: A Unified Foundation, http://link.springer.com/book/10.1007/978-1-4612-4138-6 **B. Liskov, Keynote address - data abstraction and hierarchy, OOPSLA '87 Proceedings Addendum, 17-34. B. Liskov and J. M. Wing. "A Behavioral Notion of Subtyping," ACM Trans. Programming Languages and Systems 16(6):1811-1841, Nov 1994.
  • 66.
    n-ary operations mylist =ListOfNumbers(); mylist.push!(1.0);
  • 67.
    n-ary operations Methods areassociated with classes. mylist = ListOfNumbers(); mylist.push!(1.0);
  • 68.
    n-ary operations Methods areassociated with classes. mylist = ListOfNumbers(); mylist.push!(1.0); A unary operation on a class can be implemented as a method of that class. The abstraction of classes can break down for functions involving multiple classes.
  • 69.
    n-ary operations Neither. Matrix+ DiagonalMatrix needs to access private fields, thus breaking encapsulation*. *A loaded term; see D. J. Armstrong, The quarks of object-oriented development, Commun. ACM 49 (2): 123-8, Feb 2006, doi:10.1145/1113034.1113040
  • 70.
    n-ary operations Adding adiagonal matrix and an ordinary (dense) matrix: is it a method of Matrix or DiagonalMatrix? Neither. Matrix + DiagonalMatrix needs to access private fields, thus breaking encapsulation*. *A loaded term; see D. J. Armstrong, The quarks of object-oriented development, Commun. ACM 49 (2): 123-8, Feb 2006, doi:10.1145/1113034.1113040
  • 71.
    n-ary operations Adding adiagonal matrix and an ordinary (dense) matrix: is it a method of Matrix or DiagonalMatrix? class Matrix { private: int m, n; float *data; // … friend Matrix operator+(Matrix M, DiagonalMatrix D); //non-member function }; class DiagonalMatrix{ private: int n; //store only the diagonals float *diagonaldata; // … friend Matrix operator+(Matrix M, DiagonalMatrix D); //non-member function }; Neither. Matrix + DiagonalMatrix needs to access private fields, thus breaking encapsulation*. *A loaded term; see D. J. Armstrong, The quarks of object-oriented development, Commun. ACM 49 (2): 123-8, Feb 2006, doi:10.1145/1113034.1113040