Você está na página 1de 9

DESENVOLVIMENTO DE UMA TOOLBOX PARA

MATLAB/OCTAVE COM BASE NA ESTRUTURA


MATEMÁTICA DE MULTI-LINK

N. MARTINS-FERREIRA

Abstract. Este trabalho consiste em desenvolver as rotinas de-


scritas no artigo [1] de modo a obter uma implementação em Mat-
lab/Octave ou outro com o objetivo de criar uma toolbox que per-
mita agilizar a programação de soluções reais com aplicação na
impressão 3D.

As seguintes rotinas devem ser implementadas :


(1) Dada uma função f : A → Rn na qual A é um conjunto finito
de ı́ndices, obter um conjunto B e funções
n
A_ >
R
q
r  u
B
tais que uq = f , f r = u, qr = 1B e u é uma função injetiva.
Neste caso a função unique.m pode ser usada para o efeito.
1 >> help unique
2 unique S e t unique .
3 C = unique (A) f o r t h e a r r a y A r e t u r n s t h e same
v a l u e s a s i n A but with
4 no r e p e t i t i o n s . C w i l l be s o r t e d .
5
6 C = unique (A, ’ rows ’ ) f o r t h e matrix A r e t u r n s t h e
unique rows o f A.
7 The rows o f t h e matrix C w i l l be i n s o r t e d o r d e r .
8
9 [ C, IA , IC ] = unique (A) a l s o r e t u r n s i n d e x v e c t o r s
IA and IC such t h a t
10 C = A( IA ) and A = C( IC ) .
11
12 [ C, IA , IC ] = unique (A, ’ rows ’ ) a l s o r e t u r n s i n d e x
v e c t o r s IA and IC such
13 t h a t C = A( IA , : ) and A = C( IC , : ) .
14
15 [ C, IA , IC ] = unique (A,OCCURRENCE) and

Date: March 8, 2023; CDRSP-IPLeiria Technical Report, GTLab(Multi-links-1)


123 (2023) 1-9.
email:martins.ferreira@ipleiria.pt.
1
2 N. MARTINS-FERREIRA

16 [ C, IA , IC ] = unique (A, ’ rows ’ ,OCCURRENCE) s p e c i f y


which i n d e x i s r e t u r n e d
17 i n IA i n t h e c a s e o f r e p e a t e d v a l u e s ( o r rows ) i n
A. The d e f a u l t v a l u e
18 i s OCCURENCE = ’ f i r s t ’ , which r e t u r n s t h e i n d e x
of the f i r s t occurrence
19 o f each r e p e a t e d v a l u e ( o r row ) i n A, while
OCCURRENCE = ’ l a s t ’ r e t u r n s
20 t h e i n d e x o f t h e l a s t o c c u r r e n c e o f each r e p e a t e d
v a l u e ( o r row ) i n A.
21
22 [ C, IA , IC ] = unique (A, ’ s t a b l e ’ ) r e t u r n s t h e v a l u e s
o f C i n t h e same o r d e r
23 t h a t they appear i n A, while [ C, IA , IC ] = unique (A
, ’ sorted ’ ) r e t u r n s the
24 v a l u e s o f C i n s o r t e d o r d e r . I f A i s a row v e c t o r
, then C w i l l be a row
25 v e c t o r a s w e l l , o t h e r w i s e C w i l l be a column
v e c t o r . IA and IC a r e
26 column v e c t o r s . I f t h e r e a r e r e p e a t e d v a l u e s i n A
, then IA r e t u r n s t h e
27 i n d e x o f t h e f i r s t o c c u r r e n c e o f each r e p e a t e d
value .
28
29 [ C, IA , IC ] = unique (A, ’ rows ’ , ’ s t a b l e ’ ) r e t u r n s t h e
rows o f C i n t h e same
30 o r d e r t h a t they appear i n A, while [ C, IA , IC ] =
unique (A, ’ rows ’ , ’ s o r t e d ’ )
31 r e t u r n s t h e rows o f C i n s o r t e d o r d e r .
32
33 The b e h a v i o r o f unique has changed . This
includes :
34 − o c c u r r e n c e o f i n d i c e s i n IA and IC s w i t c h e d
from l a s t t o f i r s t
35 − IA and IC w i l l always be column i n d e x v e c t o r s
36
37 I f t h i s change i n b e h a v i o r has a d v e r s e l y a f f e c t e d
your code , you may
38 p r e s e r v e t h e p r e v i o u s b e h a v i o r with :
39
40 [ C, IA , IC ] = unique (A, ’ l e g a c y ’ )
41 [ C, IA , IC ] = unique (A, ’ rows ’ , ’ l e g a c y ’ )
42 [ C, IA , IC ] = unique (A,OCCURRENCE, ’ l e g a c y ’ )
43 [ C, IA , IC ] = unique (A, ’ rows ’ ,OCCURRENCE, ’
legacy ’)
44
45 Examples :
46
47 a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2
1]
48
49 [ c1 , i a 1 , i c 1 ] = unique ( a )
MULTI-LINKS TOOLBOX 3

50 % returns
51 c1 = [ 1 2 4 5 6 7 8 9 ]
52 i a 1 = [ 2 1 20 19 18 16 13 10 6 ]
53 ic1 = [8 8 8 8 8 8 7 7 7 7 6 6 6 5 5 5 4 4 3
2 1]
54
55 [ c2 , i a 2 , i c 2 ] = unique ( a , ’ s t a b l e ’ )
56 % returns
57 c2 = [ 9 8 7 6 5 4 2 1 ]
58 i a 2 = [ 1 7 11 14 17 19 20 2 1 ] ’
59 ic2 = [1 1 1 1 1 1 2 2 2 2 3 3 3 4 4 4 5 5 6
7 8] ’
60
61 c = unique ( [ 1 NaN NaN 2 ] )
62 % NaNs compare as not e q u a l , so t h i s r e t u r n s
63 c = [ 1 2 NaN NaN]
64
65 C l a s s s u p p o r t f o r input A:
66 − l o g i c a l , char , a l l numeric c l a s s e s
67 − c e l l arrays of strings
68 −− ’ rows ’ o p t i o n i s not s u p p o r t e d f o r c e l l
arrays
69 − o b j e c t s with methods SORT (SORTROWS f o r t h e
’ rows ’ o p t i o n ) and NE
70 −− i n c l u d i n g h e t e r o g e n e o u s a r r a y s
71
72 See a l s o union , i n t e r s e c t , s e t d i f f , s e t x o r ,
ismember , sort , s o r t r o w s .

(2) Dado um digraph na forma


f
/
A / Rn
g

no qual A é um conjunto finito de ı́ndices (f associa a cada


ı́ndice o seu vértice de origem enquanto que g associa a cada
ı́ndice o seu vértice de destino), obter uma sua representação
na forma

n
A >
R
s


t u
B
tal que us = f , ut = g e u é uma função injetiva.
A implementação deve ter a seguinte assinatura:
1 function [ s , t , u]= d i g r a p h ( s o u r c e , t a r g e t )

(3) Dado um digraph na forma


s /
A / B (1)
t
4 N. MARTINS-FERREIRA

no qual ambos A e B são conjuntos finitos de ı́ndices (tal


como obtido pelo procedimento digraph), obter um endofunção
ϕ : A → A tal que sϕ = t.
Nesta situação existem muitas soluções possı́veis e espera-se
que seja feito um estudo sobre as variadas soluções e as suas
particularidades explorando diferentes propriedades.
A implementação deve ter a seguinte assinatura:
1 function p h i=l i n k d i g r a p h (A, s , t )

A utilidade de ter a variável A para além de s e t é a de poder


considerar subgrafos.
(4) A estrutura obtida no item anterior é designada por link, ver
referência [1] na secção 3, e em particular iremos trabalhar com
o caso em que o vértices são números complexos, a seguinte
implementação serve para visualizar uma tal estrutura quando
apresentada na forma
g
ϕ A / C. (2)
7
1 function h=l i n k d r a w (A, phi , g , v a r a r g i n )
2 %h=l i n k d r a w (A, phi , g , lambda )
3 %
4 % phi , g and lambda a r e v e c t o r o f t h e same s i z e
5 % A i s a s u b s e t o f t h e n a t u r a l numbers used as
indexes
6 % each e l e m e n t o f p h i (A) must be t h e i n A
7 %
8 % Example
9 % A= 1 :7 ; lambda=A;
10 % g =[1 , 3+i , 5 , 3+6 i , 4+2 i , 2+2 i , 3+4 i ] ;
11 % p h i =[2 3 4 1 6 7 5 ] ;
12 %
13 % I f v a r a r g i n i s a number b e t w e e n 0 and 1 draw t h e
c i r c l e i l l u s t r a t i n g a n g l e s between c o n s e c u t i v e
arrows
14 %
15 % Example
16 % t =. 55 ; A=[1 2 3 4 5 6 ] ; p h i =[2 3 4 5 6 1 ] ; g =[0 1 t
∗(1+0.5 i ) 1+1 i .25+.75 i 1 i ] ; lambda =[1 2 3 4 5 6 ] ;
17 % l i n k d r a w (A, phi , g , lambda , . 1 5 ) ;
18
19 A=A ( : ) ; % e n s u r e t h a t A i s a colum v e c t o r
20 t r y
21 phiA=p h i (A) ; % r e s t r i c t p h i t o domain A
22 gA=g (A) ; % r e s t r i c t g t o t h e domain A
23 gphiA=g ( phiA ) ;
24 %lambdaA=lambda (A) ;
25 c a t c h ME
26 error (ME. message )
27 end
MULTI-LINKS TOOLBOX 5

28 dirA=gphiA−gA ; midA=(gA+gphiA ) / 2 ;
29 x=r e a l (gA) ; y=imag (gA) ;
30 u=r e a l ( dirA ) ; v=imag ( dirA ) ;
31 h=quiver ( x , y , u , v , 0 ) ;
32
33 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

34 % t h i s code i s e x e c u t e d i f t h e parameter lambda=


v a r a r g i n {1} i s p r o v i d e d
35 i f nargin > 3
36 lambdaA=v a r a r g i n { 1 } ;
37 text ( r e a l ( midA ) , imag ( midA ) , num2str ( lambdaA ( : ) ) )
38 end
39
40 %
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

41 % t h i s code i s o n l y e x e c u t e d i f 5=3+2 p a r a m e t e r s a r e
entered
42 i f nargin > 4
43 v=v a r a r g i n { 2 } ;
44 c e n t e r=g ( p h i (A ( : ) ) ) ;
45 incom=g (A ( : ) )−c e n t e r ;
46 outcom=g ( p h i ( p h i (A ( : ) ) ) )−c e n t e r ;
47 r a d i u s=v∗mean( abs ( [ incom ( : ) ; outcom ( : ) ] ) ) ;
48 a n g b e g i n=angle ( incom ) ; % where t h e a n g l e b e g i n s
49 ang amp=angle ( outcom . / incom ) ; % a m p l i t u d e
50 ang amp ( ang amp <0)=ang amp ( ang amp <0)+2∗pi ; %
transform in [0 2 pi [
51 dg =(( ang amp/ pi ) ∗ 1 8 0 ) ; % t r a n s f o r m s i n t o d e g r e e s
t i m e s 10 (0−−36) i n s t e a d o f (0−−360)
52 disp ( [ A ( : ) , a n g b e g i n ( : ) , ang amp ( : ) , dg ( : ) ] )
53 dg=round ( dg ) ;
54 hold on
55 % p l o t a n g l e s ( as a r c w i t h r a t i u s v − t h e e n t e r e d
parameter ) a t t a c c h e d t o e v e r y arrow and i t s
s u c c e s i v e arrow
56 f o r j =1: numel (A)
57 z0=c e n t e r ( j ) ;
58 t=linspace ( 0 , 1 , abs ( dg ( j ) ) / 1 0 ) ;
59 a n g p o i n t s=r a d i u s ∗exp ( ( t ∗ ( ang amp ( j ) )+a n g b e g i n ( j )
) ∗ sqrt ( −1) ) ;
60 plot ( z0+a n g p o i n t s , ’ . ’ )
61 i f abs ( dg ) >5,
62 tx=z0+a n g p o i n t s ( round ( ( ( end+1) / 2 ) ) ) ; %c e n t e r+
v ∗ exp ( ( 0 . 5 ∗ ( ang amp )+a n g b e g i n ) ∗ s q r t (−1) ) ;
63 text ( r e a l ( tx ) , imag ( tx ) , num2str ( dg ( j ) ) )
64 end
65 end
66 axis e q u a l
67 hold o f f
6 N. MARTINS-FERREIRA

68 end

Esta implementação é apenas uma base de partida e pode ser


alterada mediante as necessidades que surgirem no âmbito de
cada grupo de trabalho.
(5) Dada uma endofunção ϕ : A → A na qual A é um conjunto finito
de ı́ndices, obter uma função q : A → B com B um conjunto
apropriado tal que as seguintes duas condições se verifiquem:
(a) qϕ = q; (b) para toda a função f : A → X (com um qual-
quer conjunto X de codomı́nio) se f ϕ = f então existe uma
única função f¯: B → X tal que f¯q = f . Tal como ilustrado no
seguinte diagrama
ϕ q
A / A / B (3)

f 
X
Esta função pode ser implementada de acordo com a seguinte
assinatura, no entando, cada grupo é livre de fazer as alterações
que entender apropriadas.
1 function [ orb , ord , p s i ]= o r b i t s ( p h i )
2 % [ orb , ord , p s i ]= o r b i t s ( p h i )
3 % The i n p u t p h i i s a column v e c t o r o f l e n g t h n w i t h
e n t r i e s from 1 t o n
4 % t h e o u t p u t s a r e v e c t o r t h e same s i z e as p h i such
t h a t f o r each a1 , a2 from 1 t o n :
5 % (A) o r b ( a1 )==o r b ( a2 ) i f and o n l y i f a1 and a2 a r e
i n t h e same o r b i t o f phi , t h a t i s , t h e r e e x i s t s k
such t h a t e i t h e r a2=p h i ˆ k ( a1 ) or a1=p h i ˆ k ( a2 ) ;
6 % (B) i f o r b ( a1 )==o r b ( a2 ) t h e n ord ( a1 )<ord ( a2 ) or ord
( a2 )<ord ( a1 ) a c c o r d i n g l y as w h e t h e r a2=p h i ˆ k ( a1 )
or a1=p h i ˆ k ( a2 ) ; and e x c e p t i o n i s made on ord ( a )
i n t h e c a s e s when ord ( a ) i s t h e g r e a t e s t e l e m e n t
o f t h e component , i n t h a t c a s e i t w i l l be I n f (
when o r b ( a )==o r b ( p h i ( a ) ) ) or e q u a l t o t h e v a l u e i n
o r b ( p h i ( a ) ) when o r b ( a )˜=o r b ( p h i ( a ) ) ; t h i s i s
c o n v e n i e n t t o s e p a r a t e b e t w e e n p r i n c i p a l ( non
c y c l i c ) components and s e c o n d a r y components −−−
t h e p r i n c i p a l ones end i n I n f whereas t h e
s e c o n d a r y ones end w i t h an i d e n t i f i e r t o t h e
p r i n c i p a l component t o which t h e y a r e c o n n e c t e d t o
;
7 % (C) p s i i s t h e pseudo i n v e r s e o f p h i w i t h Nan
i n d e n t i f y i n g t h e p o i n t s w i t h more than one c h o i c e
f o r t h e i n v e r s e ( j u n c t i o n p o i n t s ) and 0
i d e n t i f y i n g t h o s e p o i n t s which do not have an
inverse ( i n i t i a l points ) .
8 %
9 % Example :
10 % p h i =[2 3 4 5 1 1 6 6 1 3 12 13 14 15 12 13 16 1 7 ] ’ ;
MULTI-LINKS TOOLBOX 7

11 % [ orb , ord , p s i ]= o r b i t s ( p h i ) ; i d p h i =(1: numel ( p h i ) ) ’ ;


12 % d i s p ( [ i d p h i p h i o r b ord p s i ] )
13 % 1 2 −1 3 NaN
14 % 2 3 −1 4 1
15 % 3 4 −1 5 NaN
16 % 4 5 −1 6 3
17 % 5 1 −1 Inf 4
18 % 6 1 −1 2 NaN
19 % 7 6 −1 1 0
20 % 8 6 −2 −1 0
21 % 9 1 −3 −1 0
22 % 10 3 −4 −1 0
23 % 11 12 −5 1 0
24 % 12 13 −5 2 NaN
25 % 13 14 −5 3 NaN
26 % 14 15 −5 4 13
27 % 15 12 −5 Inf 14
28 % 16 13 −6 −5 17
29 % 17 16 −6 2 18
30 % 18 17 −6 1 0
31 %
32 %
The f o l l o w i n g i n f o r m a t i o n can be e x t r a c t e d :
33 i d p h i ( i s n a n ( p s i ) ) −−− g i v e s a l l t h e j u n t i o n p o i n t s
%
34 i d p h i ( p s i ==0) −−− g i v e s a l l t h e i n i t a l p o i n t s
%
35 xa=ord ( o r b==o r b ( a ) ) −−−− g i v e s a l l t h e p o i n t s i n
%
t h e same component as a
36 % [ ˜ , sa ]= s o r t ( xa ) −−− g i v e s t h e p o i n t s i n t h e same
component as a l i n e a r l y o r d e r e d as an o r b i t ,
p r o v i d e d o r b ( a ) i s not a s e c o n d a r y component , f o r
example i f a==1 t h e n xa i s [ 3 4 5 6
Inf 2 1 ] w h i l e sa i s [ 7 6 1 2 3
4 5]
37 % f o r s e c o n d a r y components we need t o have i n t o
account the f a c t t h a t the l a s t point in the o r b i t
i s encoded w i t h a n e g a t i v e number i n d i c a t i n g t h e
p r i n c i p a l component t o which i t i s c o n n e c t e d t o
38 %
39 % when c y c l i c components a r e added we o b s e r v e :
40 % [ orb , ord , p s i ]= o r b i t s ( p h i ) ;
41 % d i s p ( [ ( 1 : numel ( p h i ) ) ’ , p h i ( : ) , orb , ord , p s i ] ) ;

Nesta implementação sugerida, para além das órbitas expres-


sas na variável orb, a qual corresponde à função q no diagrama
(3) apresenta-se também a variável ord a qual expressa uma
ordenação dentro de cada órbita disjunta e a variável psi que
representa uma inversa à esquerda para a função ϕ.
(6) Implementar uma função que possa ser utilizada para verificar
se a função orbits.m está de acordo com as suas especificações,
por exemplo com a seguinte assinatura.
1 function [ phi , orb , ord , p s i ]= o r b i t s g e n ( n , k , ck , pk )
2 % [ phi , orb , ord , p s i ]= o r b i t s g e n ( n , k , ck , pk )
8 N. MARTINS-FERREIRA

3 %
4 %
5 % Example :
6 % n=20 , k =7, ck =2, pk =2,
7 % [ phi , orb , ord , p s i ]= o r b i t s g e n ( n , k , ck , pk ) ;
8
9 >> disp ( [ ( 1 : 2 0 ) ’ p h i orb ord p s i ] )
10 1 2 −1 1 0
11 2 3 −1 2 1
12 3 4 −1 3 2
13 4 3 −1 Inf 3
14 5 6 −2 1 0
15 6 7 −2 2 5
16 7 8 −2 3 6
17 8 8 −2 Inf 7
18 9 10 −3 1 0
19 10 11 −3 2 9
20 11 12 −3 3 10
21 12 8 −3 −2 11
22 13 14 −4 1 0
23 14 15 −4 2 13
24 15 2 −4 −1 14
25 16 17 −5 1 0
26 17 8 −5 −2 16
27 18 18 1 1 18
28 19 20 2 1 20
29 20 19 2 2 19

Neste caso, n é o número total de elementos no conjunto


de domı́nio da função ϕ, k o número total de componentes,
das quais ck são cı́clicas, pk são principais, e as restantes são
secundárias.
(7) Implementar uma função que faça a visualização das órbitas no
plano complexo.

1 function g=o r b i t s v i z ( phi , orb , ord )

Sendo que a variável g é um vetor com as coordenadas com-


plexas que dá origem à visualização no plano complexo.
(8) Dada uma estrutura de link na forma

g
ϕ A / (4)
7 C

a qual é assumida como representando uma região bem definida


do plano complexo pelo ser contorno, implementar uma rotina
que calcula um offset x em relação à região original.

1 function gx=l i n k o f f s e t (A, phi , g , x )


MULTI-LINKS TOOLBOX 9

2 % Given a l i n k s t r u c t u r e A, phi , g w i t h o n l y one


c y c l i c component and an o f f s e t v a l u e x , r e t u r n s
t h e new c o o r d i n a t e s gx o f t h e l i n k s t r u c t u r e . I f x
i s negative the o f f s e t goes into the i n t e r i o r of
t h e r e g i o n d e f i n e d by t h e curve , i f x i s p o s i t i v e
t h e n t h e o f f s e t g o e s t o i t s e x t e r i o r r e g i o n . The
c o n n e c t i v i t y o f t h e l i n k , t h a t i s , phi , d o e s not
change

(9) O problema da interpolação. Dados dois links na forma


g
ϕ A / (5)
7 C
e
g0
0 /C
6A
ϕ0 (6)
0
encontrar uma função f : A → A tal que para cada a ∈ A ex-
istem k, k 0 ∈ N que satisfazem a equação
0
g 0 (ϕ0k (f (a))) = g(ϕk a)
Os detalhes devem ser explorados pelos grupos tendo em atenção
o exemplo de aplicação que se pretende ser o seguinte. Dada
uma região sólida no espaço, a qual foi seccionada por dois
planos, dando origem aos dois links (A, ϕ, g) e (A0 , ϕ0 , g 0 ) o
problema da interpolação pretende reconstruir o mais aprox-
imadamente possı́vel a região sólida original a partir das duas
secções dadas.
(10) Implementar o algoritmo isoslice tal como descrito na referência
[1] na secção 3.6.
Os tópicos (1)-(8) são coniderados necessários para a obtenção da
nota mı́nima e terão acompanhamento nas aulas práticas.
A resolução do item (9) dá origem a uma nota máxima de 14 valores.
Para notas superiores a 14 valores o item (10) deve ser resolvido
o mais autonomamente possı́vel. Em alternativa, cada grupo poderá
propor uma tarefa própria, a qual sendo considerada equivalente, pode
ser desenvolvida para o mesmo efeito.
References
[1] N. Martins-Ferreira, Internal Categorical Structures and Their Applications,
Mathematics 11 (3) (2023) 660.
https://www.mdpi.com/2227-7390/11/3/660

Você também pode gostar