I have been dealing many days with a problem without any success. In my programm I define a list, let's call it "L", by shifting a single geometrical object, a circle, many times. So L is composed by manu circles. The object circle is also a list which contains its properties: center (center . #V), (height . H), radius (radius . R), and so on. So, the property radius is a pair in the 3rd position of the list circle. If I do (object-property-value circle 'radius) = R. Now, what I want to do is to create a new list, L-disorder, composed by circles with the same positions of those of L but each with different (random) radius. Then, I try this:
(define L-disorder (map
(lambda(obj)
(set-cdr! (list-ref obj 3) (random:normal))
obj)
L))
My problem is that it changes the radius of all the circles the same way! And I want a different (random) value for all of them.
I would really thank any help or advise!!
If you want to create new list, you shouldn't use mutation functons (set-cdr!).
(map ) functions do all the magic for you: it iterates over source list, and creates new list.
(define L-disorder
(map (lambda (circle)
; here we creating new circle
(list (car circle) (cadr circle) (random:normal)))
L))
Related
I am trying to check if a point is in a cross shape. That looks like this
Not sure how to modify the pnpoly to suit this type of shape
bool PointInPolygon(vector<pair<int,int>> points,int x, int y) {
int i, j, nvert = points.size();
bool c = false;
for(i = 0, j = nvert- 1; i < nvert; j = i++) {
if( ( (points[i].second >= y) != (points[j].second >= y) ) &&
(x <= (points[j].first - points[i].first) * (y - points[i].second) / (points[j].second - points[i].second) + points[i].first)
)
c = !c;
}
Appreciate any help
While googling reveals, that the term "scanline" has gotten a new meaning recently, it used to be the name for an algorithm to detect intersections of lines and other geometric shapes.
The algorithms basic idea is to move a vertical "scan line" across the vertices from left to right and to record the vertical boundaries of the shape at that very x-coordinate (where the scanline is currently positioned).
Given you only have horizontal and vertical lines, the implementation of that idea can simply jump from x-coordinate of a vertex to the next, if the vertices are sorted in ascending order on the x-coordinate.
Since multiple points can share the same x-coordinate, all we need to note is, at that very x-coordinate, which are the highest and lowest y coordinates of those vertices, respectively.
Given that 32degree Celsius summertime heat, I was too lazy to create a C++ version of it. Instead, some quick hacking in Common Lisp. But it should be easy enough to read and translate. Instead of defining some kind of point structure, I simply took complex numbers, where your C++ program uses a std::pair<int,int>. realpart/imagpart obtain the real or imaginary part of a complex number, which, here is simply the x and y component of the vertex.
(defparameter *shape* '(#C(3 3) #C(3 6)
#C(5 6) #C(5 9)
#C(6 9) #C(6 6)
#C(6 3) #C(6 0)
#C(8 0) #C(8 3)
#C(11 3) #C(11 6)))
(defun point-in-shape-p (shape point)
(let ((x-sorted (sort
shape
#'(lambda (p1 p2)
(< (realpart p1)
(realpart p2))))))
(if (< (realpart point)
(realpart (first x-sorted)))
nil
(loop
with x-pivot = -1000000 ;; outside
with y-high = -1000000 ;; outside
with y-low = 100000 ;; outside
for v in x-sorted
when (> (realpart v) x-pivot)
do (progn
(setf x-pivot (realpart v))
(setf y-high -1000000)
(setf y-low 1000000))
when (> (imagpart v) y-high)
do (setf y-high (imagpart v))
when (< (imagpart v) y-low)
do (setf y-low (imagpart v))
when (and (<= (realpart point) x-pivot)
(<= (imagpart point) y-high)
(>= (imagpart point) y-low))
return t))))
Here a few test runs, I did for various points and the shape from your image:
CL-USER> (point-in-shape-p *shape* #C(7 8))
NIL
CL-USER> (point-in-shape-p *shape* #C(7 4))
T
CL-USER> (point-in-shape-p *shape* #C(9 2))
NIL
CL-USER> (point-in-shape-p *shape* #C(9 3))
T
No warranties, but I think it is looking good.
I have the following function that calculates an area.
It receives three parameters, the first is an n representing the number of cases, the 2nd representing the radio of circumferences, and the 3rd is l giving me back the result.
The problem I have is that when the 1st input is greater than 1 it does not work.
This my code:
as(1, [X], A) :-
A is (sqrt(3.0) * (X*X)) - (3.14 * (X*X))/2.
as(N, [H|_T], A) :-
A is (sqrt(3.0) * (H*H)) - (3.14 * (H*H))/2,
N1 is N-1,
as(N1-1, T, A).
An example of how it should work is:
?- as(4, [1,1,1,1], R).
R = 0.162050807568877130000 ;
R = 0.162050807568877130000 ;
R = 0.162050807568877130000 ;
R = 0.162050807568877130000.
If you could help me, I would be grateful ...
Is there a reason this version isn't sufficient to your needs?
as([H|_], A):-
A is (sqrt(3.0) * (H*H)) - (3.14 * (H*H))/2.
as([_|T], A) :- as(T, A).
Or maybe this?
area(H, Area) :-
Area is (sqrt(3.0) * (H*H)) - (3.14 * (H*H))/2.
as(List, Area) :- member(Ratio, List), area(Ratio, Area).
I don't understand why you need to worry about N at all.
Matching both N and [X] leads to redundancy. You shouldn't have to repeat your formula.
You have a lot of singleton errors: _T in the head, and then T in the body which will not work.
You are passing N1-1 to the recursive call, which will not cause it to be evaluated—but you already evaluated N-1 in the previous expression, so just pass N1 here. Again, I don't see the point of this at all.
I think it's a good idea to use succ(N1, N) instead of adding or subtracting one, because it works in both directions (not relevant here, of course).
It feels a bit icky to be combining the list traversal with the calculation to me. I would in general break things down so that the calculation is separate from the data structure insofar as that can be done. This is a universal maxim of programming.
Since you want to compute the area for every measurement anyway, would it not be opportune to get a list of areas corresponding to the list of radio measurements? The structure of your predicate as/3 seems to indicate that you were thinking along those lines. And you could easily achieve that by using maplist/3:
:- use_module(library(apply)). % needed for maplist
% a single measurement and the corresponding area
area(X, A) :-
A is (sqrt(3.0) * (X*X)) - (3.14 * (X*X))/2.
areas(Xs,As) :-
maplist(area,Xs,As). % area/2 mapped to Xs results in As
Querying this predicate yields the desired results but in a list:
?- areas([1,1,5,3],As).
As = [0.16205080756887713, 0.16205080756887713, 4.051270189221931, 1.4584572681198935].
I have lists similar to this :
'(
; element 1
(
((X hello))
)
; element 2
(
((X hello)(Y world))
((X hello) (Y earth))
((X hi) (Y planet))
)
; element 3
(
((Y world))
)
)
I'd like to make a loop or something that can give me an "intersection" of the elements in that list. For exemple for the list above the result should be something like that :
((X hello) (Y world))
Indeed this result is the only one that can satisfy the X and Y. I'm very new to clojure so if you also have advices for a better data structure, they're also welcome.
You need to re-think the problem a bit. Right now, an intersection would produce the null set.
In addition, you may wish to study the built-in functions like
https://clojuredocs.org/clojure.core/set
https://clojuredocs.org/clojure.set/intersection
The Clojure CheatSheet
I want to draw an sketch with eight pattern. Now I know how to draw circles in counterclockwise and clockwise directions. But I do not know how to combine them.
(defn draw-state [state]
(let [x (* 150 (quil.core/cos angle))
y (* 150 (quil.core/sin angle))]
(quil.core/ellipse x y 100 100)
(quil.core/ellipse y x 100 100)))
This function will draw two circles in opposite directions. But how to draw a sketch with 8 pattern?
A polar equation for an 8-type of curve =
r^2 = Cos[2t] (Sec[t])^4
where r = radius, t = angle
You could start with this.
I'm trying to implement a simple logistic regression example in Clojure using the Incanter data analysis library. I've successfully coded the Sigmoid and Cost functions, but Incanter's BFGS minimization function seems to be causing me quite some trouble.
(ns ml-clj.logistic
(:require [incanter.core :refer :all]
[incanter.optimize :refer :all]))
(defn sigmoid
"compute the inverse logit function, large positive numbers should be
close to 1, large negative numbers near 0,
z can be a scalar, vector or matrix.
sanity check: (sigmoid 0) should always evaluate to 0.5"
[z]
(div 1 (plus 1 (exp (minus z)))))
(defn cost-func
"computes the cost function (J) that will be minimized
inputs:params theta X matrix and Y vector"
[X y]
(let
[m (nrow X)
init-vals (matrix (take (ncol X) (repeat 0)))
z (mmult X init-vals)
h (sigmoid z)
f-half (mult (matrix (map - y)) (log (sigmoid (mmult X init-vals))))
s-half (mult (minus 1 y) (log (minus 1 (sigmoid (mmult X init-vals)))))
sub-tmp (minus f-half s-half)
J (mmult (/ 1 m) (reduce + sub-tmp))]
J))
When I try (minimize (cost-func X y) (matrix [0 0])) giving minimize a function and starting params the REPL throws an error.
ArityException Wrong number of args (2) passed to: optimize$minimize clojure.lang.AFn.throwArity (AFn.java:437)
I'm very confused as to what exactly the minimize function is expecting.
For reference, I rewrote it all in python, and all of the code runs as expected, using the same minimization algorithm.
import numpy as np
import scipy as sp
data = np.loadtxt('testSet.txt', delimiter='\t')
X = data[:,0:2]
y = data[:, 2]
def sigmoid(X):
return 1.0 / (1.0 + np.e**(-1.0 * X))
def compute_cost(theta, X, y):
m = y.shape[0]
h = sigmoid(X.dot(theta.T))
J = y.T.dot(np.log(h)) + (1.0 - y.T).dot(np.log(1.0 - h))
cost = (-1.0 / m) * J.sum()
return cost
def fit_logistic(X,y):
initial_thetas = np.zeros((len(X[0]), 1))
myargs = (X, y)
theta = sp.optimize.fmin_bfgs(compute_cost, x0=initial_thetas,
args=myargs)
return theta
outputting
Current function value: 0.594902
Iterations: 6
Function evaluations: 36
Gradient evaluations: 9
array([ 0.08108673, -0.12334958])
I don't understand why the Python code can run successfully, but my Clojure implementation fails. Any suggestions?
Update
rereading the docstring for minimize i've been trying to calculate the derivative of cost-func which throws a new error.
(def grad (gradient cost-func (matrix [0 0])))
(minimize cost-func (matrix [0 0]) (grad (matrix [0 0]) X))
ExceptionInfo throw+: {:exception "Matrices of different sizes cannot be differenced.", :asize [2 1], :bsize [1 2]} clatrix.core/- (core.clj:950)
using trans to convert the 1xn col matrix to a nx1 row matrix just yields the same error with opposite errors.
:asize [1 2], :bsize [2 1]}
I'm pretty lost here.
I can't say anything about your implementation, but incanter.optimize/minimize expects (at least) three parameters and you're giving it only two:
Arguments:
f -- Objective function. Takes a collection of values and returns a scalar
of the value of the function.
start -- Collection of initial guesses for the minimum
f-prime -- partial derivative of the objective function. Takes
a collection of values and returns a collection of partial
derivatives with respect to each variable. If this is not
provided it will be estimated using gradient-fn.
Unfortunately, I'm not able to tell you directly what to supply (for f-prime?) here but maybe someone else is. Btw, I think the ArityException Wrong number of args (2) passed to [...] is actually quite helpful here.
Edit: Actually, I think the docstring above is not correct, since the source code does not use gradient-fn to estimate f-prime. Maybe, you can use incanter.optimize/gradient to generate your own?
First, Your cost function should have a parameter for theta like your python implementation however your implementation has fixed as initial-value.
Second, if your cost-func is correct, then you can call optimize like this
(optimize (fn [theta] (cost-func theta X y)) [0 0 0])
Hope this can help you.