Julia: Affine Transformations with Compose module - julia

Is it possible to transform images (as shown below) via matrices with Compose.jl?
If so, could you please provide a simple example?
I'm aware of the rotation keyword argument in the Compose.context method, but I was wondering if there was something similar for general affine transformations. Thanks!

You can use Shear. For example, you can transform
from the tutorials (code below)
julia> composition = compose(context(),
(context(units=UnitBox(0, 0, 1000, 1000)),
polygon([(0, 1000), (500, 1000), (500, 0)]),
fill("tomato")),
(context(),
polygon([(1, 1), (0.5, 1), (0.5, 0)]),
fill("bisque")))
and shear it with
julia> composition_sheared = compose(context(shear=Shear(0.3,0.0,0.5,1.0)),
(context(units=UnitBox(0, 0, 1000, 1000)),
polygon([(0, 1000), (500, 1000), (500, 0)]),
fill("tomato")),
(context(),
polygon([(1, 1), (0.5, 1), (0.5, 0)]),
fill("bisque")))
to obtain

Related

Using igraph subgraph_isomorphisms to find given network motifs

I'm looking for motifs of size 5 in graphs with less than 5000 nodes and less than 10000 edges. (everything uncolored)
To do this I use function provided in igraph library for R subgraph_isomorphisms using method vf2 (see example below). I use adjacency matrix to generate subgraph and edgelist to generate the graph itself.
A lot of isomorphic subgraphs that I find have extra edges. Is there any way to only find subgraphs with exact given structure? Looking for answers using igraph or any other library in R
See reproducible example below (looking at this example is way easier if you just draw graph given by this adjacency matrix on a piece of paper)
library(igraph)
subgraph <- matrix(
data = c(0, 1,
1, 0), ncol = 2)
graph <- matrix(
data = c(0, 1, 0, 0,
1, 1, 0, 1,
1, 0, 0, 1,
0, 0, 1, 0), ncol = 4)
subgraph <- graph_from_adjacency_matrix(subgraph, mode = "directed", weighted = T, diag = T)
graph <- graph_from_adjacency_matrix(graph, mode = "directed", weighted = T, diag = T)
subgraph_isomorphisms(subgraph, graph, method = "vf2")
Output gives you two pairs of (1,2) and (3,4), when in fact adjacency matrix of (1,2) looks like
(0 1)
(1 1)
Which is different from the one we were looking for
The answer to this question is in definitions of what I'm looking for and what I'm finding.
What I was looking for was network motifs of size 5. When I'm looking for network motifs from the graph theory perspective it means that I'm looking for induced subgraphs with given adjacency matrix.
What this function does is it finds subgraphs of a graph that are isomorphic to a given graph. The difference is I was looking for induced subgraph, whereas the function just gives subgraphs, so extra edges are allowed.
That is exactly the problem that I was experiencing. To deal with it I ended up just comparing adjacency matrix of subgraphs that I found with those of the motif. Hope it will be helpful to someone.
Adding to the previous comment, I also noticed that the function returns "True" when I try to find an isomorphic triad of type 210 (2 mutual edges and 1 asymmetric) within a complete graph of four vertices. The solution is to add:
subgraph_isomorphisms(subgraph, graph, method = "vf2", **induced = TRUE**)

Monte Carlo integral in R : Hit or Miss with "halton"

I am struggling with the Monte Carlo integral problem in R.
y= x^2+cos(x); for x= [0,2]
I am supposed to use HitMiss <- function(T,S,method="halton") to solve this problem.
T is the number of trails run per sample size in S.
S is number of sample points
The function should return T*|S| matrix, |S| is length of S.
Please help me out and give me some clue on solving this problem.
Really appreciate it!!!
A few hints:
(1) Have you seen this package? There are some Halton methods: https://cran.r-project.org/web/packages/randtoolbox/randtoolbox.pdf
(2) Do you want to solve it only with the Halton method? For this easy example an easy solution would be this one:
set.seed(1)
N = 10000
f = function(x) x^2 + cos(x)
points(runif(N, 0, 2), runif(N, 0, 4), pch = 20)
curve(f(x), 0,2, ylim=c(0, 4), col='white', lwd = 2, add=TRUE)
sum(f(runif(N, 0, 2)) > runif(N, 0, 4))/N * 2*4
#[1] 3.5592

Maxima: how to plot a 2D and 3D vector?

What is the simplest way to plot and 2D and 3D vectors using wxMaxima?
I have searched around, but all the solutions seem too complicated just for a single plot of simple vector, is that possible?
I would like to see 3 vectors at the same time in a 3D space in order to visualize a shape, but all options I have seen around seem to make this simple task not so trivial.
Does it help?
load("draw");
draw3d(vector([0, 0, 0], [100, 0, 0]),
vector([0, 0, 0], [0, 100, 0]),
vector([0, 0, 0], [0, 0, 100]));

Why doesn't bezierPoints return unitless points?

I need to get the x- and y-coordinates of points along a Bezier curve in R. I thought this would work:
x <- c(0, 0, 1, 1)
y <- c(0, 1, 1, 0)
bg <- bezierGrob(x, y)
trace <- bezierPoints(bg)
But after running that trace$x and trace$y are a bunch of measurements in inches well outside the range of (0,1). The man page for bezierPoints says:
Rather than drawing an Xspline (or Bezier curve), this function returns the points that would be used to draw the series of line segments for the Xspline.
Am I running into some grid weirdness? Or am I trying to use the wrong solution to this problem?
Looks like the bezier package, not grid, is the way to go. This works:
t <- seq(0, 1, length=100)
p <- matrix(c(0,0, 0,1, 1,1, 1,0), nrow=4, ncol=2, byrow=TRUE)
bp <- bezier(t=t, p=p)

How to specify the margin of a plot in mm/cm using matplotlib?

Example
Suppose I have two triangles:
A triangle with points (0, 0), (10, 0), (10, 0.5) and
a triangle with points (0, 0), (1, 0), (0.5, 11)
The resulting two plots without specifying the xlim and ylimlook like this:
Question
What do I need to do to satisfy all points listed below?
Make the triangle visible, so that no line of the triangle is hidden by an axis
Specify the same margin for all plots in mm, cm or other unit.
(in the example above only two triangles were used. Actually I have n triangles.)
As margin I mean the distance between the outer points of the triangle and the axis.
The resulting plots should look like this
with the difference that the distances, which are marked with the red arrows, should all be the same!
I don't know of a way to to it in cm/mm, but you can do it with the precentage of the total size:
# you don't really need this see bellow
#from matplotlib.backends.backend_pdf import PdfPages
import pylab
import matplotlib.pyplot as plt
left,bottom,width,height = 0.2,0.1,0.6,0.6 # margins as % of canvas size
fig = plt.figure(figsize=(4,4),facecolor="yellow") # figure size in Inches
fig.patch.set_alpha(0.8) # just a trick so you can see the difference
# between the "canvas" and the axes
ax1 = plt.Axes(fig,[left,bottom,width,height])
ax1.plot([1,2,3,4],'b') # plot on the first axes you created
fig.add_axes(ax1)
ax1.plot([0,1,1,0,0], [0,0,1,1,0],"ro") # plot on the first axes you created
ax1.set_xlim([-1.1,2])
ax1.set_ylim([-1.1,2])
# pylab.plot([0,1,1,0,0], [0,0,1,1,0],"ro") avoid usig if you
# want to control more frames in a plot
# see my answer here
#http://stackoverflow.com/questions/8176458/\
#remove-top-and-right-axis-in-matplotlib-after-\
#increasing-margins/8180844#8180844
# pdf = PdfPages("Test.pdf")# you don't really need this
# pylab.savefig(pdf, papertype = "a4", format = "pdf")
# automagically make your pdf like this
pylab.savefig("Test1.pdf", papertype="a4",facecolor='y')
pylab.show()
pylab.close()
# pdf.close()
and the output is:
corrected image:
Your two triangles with points (0, 0), (10, 0), (10, 0.5) and (0, 0), (1, 0), (0.5, 11) would be represented in pylab as:
Ax = [0, 10, 10]
Ay = [0, 0, 0.5]
Bx = [0, 1, 0.5]
By = [0, 0, 11]
pylab.plot(Ax, Ay)
pylab.plot(Bx, By)
Let's see what the lowest X value is:
lowestX = None
for x in Ax+Bx:
if lowestX==None or x<lowestX:
lowestX = x
Exercise for the reader to do the same for highestX, lowestY, and highestY.
Now, consider a boundary of 2 units, you can add / subtract these units from the lowest and highest values and set xlim and ylim:
margin = 2
pylab.xlim([lowestX-margin, highestX+margin])
pylab.ylim([lowestY-margin, highestY+margin])

Resources