Help, I wrote 6 iterations of arm rotation, but I want to write this function recursively. Does anyone have any tips?
(: rotate-arm : Image Image Natural -> Image)
;; rotates the entire arm 5 times, returning the full star
(define (rotate-arm tile tile2 i)
(local
{(define arm (draw-arm tile tile2 i))}
(clear-pinhole
(overlay/pinhole
(put-pinhole
(center-x (draw-arm tile tile2 i)) 0 arm)
(rotate (* i 60) (put-pinhole
(center-x arm) 0 arm))
(rotate 60 (put-pinhole
(center-x arm) 0 arm))
(rotate 120 (put-pinhole
(center-x arm) 0 arm))
(rotate 180 (put-pinhole
(center-x arm) 0 arm))
(rotate 240 (put-pinhole
(center-x arm) 0 arm))
(rotate 300 (put-pinhole
(center-x arm) 0 arm))))))
It's correct, just inefficient.
Related
Hello I have this image:
This image has 16 pixels. White is 0, pink 1... like in image. My problem is that I need to calculate number of pixel from coordinates. If I have coordinates x = 3 and y = 3, I need to get black pixel with number 15.
How can I do that?
If you know how many pixels per row you have, you can simply use the next formula :
(y * numberPerRow) + x
Example using a 16 pixels images (4x4)
x
y
calcul
value
3
3
3 * 4 + 3
15
0
0
0 * 4 + 0
0
1
2
2 * 4 + 1
9
I have to use multinomial-dist in order to express the following distribution:
x
P(x)
red
0.5
blue
0.05
green
0.4
black
0.05
Where P(x) refers to the probability of x.
I implemented the following solution in Dr.Racket using Gamble:
(define color '("red" "blue" "green" "black"))
(define (color-probability color)
(cond
[(equal? "red") 0.5]
[(equal? "blue") 0.05]
[(equal? "green") 0.4]
[else 0.05]))
(define my-color (multinomial-dist color color-probability))
(dist-sample my-color)
But it returns an error:
make-multinomial-dist: contract violation
expected: natural?
given: '("red" "blue" "green" "black")
in: the 1st argument of
(->
natural?
(vectorof (>=/c 0))
multinomial-dist?)
I'm new in Racket and i'm still learning the basics and i don't understand what the compiler didn't like!
Thank you all!
The documentation entry for multinomial-dist, viewed from DrRacket by selecting multinomial-dist, right clicking on it, choosing Search in Help Desk for "multinomial-dist" (do this for each new function in what follows) is:
(struct multinomial-dist (n weights))
n : exact-nonnegative-integer?
weights : (vectorof (>=/c 0))
Represents a multinomial distribution. The support consists of vectors of the same length as weights representing counts of n iterated samples from the corresponding categorical distribution with weights for weights.
So a multinomial-dist can be constructed by an expression, for example, like:
(multinomial-dist 100 (vector 49 51))
(the (vector 49 51) could be the result of 100 iterated samples from a
categorical distribution with weights (vector 50 50) eg representing a coin toss)
The P(x) values in the question are categorical distribution (sometimes called
discrete distribution) weights, so start with this:
#lang racket
(require Gamble)
(define color-dist (categorical-dist (vector 0.5 0.05 0.4 0.05)))
To try this out, sample the distribution a few times in DrRacket's interaction area:
> (sample color-dist)
0
> (sample color-dist)
2
> (sample color-dist)
0
>
One way to construct iterated samples in Racket is with build-list:
(define samples (build-list 100 (lambda (x) (sample color-dist))))
> samples
'(3 0 0 2 2 0 2 0 0 0 0 0 2 0 2 0 1 2 2 2 0 2 0 2 1 0 2 0 2 0 0 0 0 2 1 0 0 2 2 2 0 1 2 0 2 2 2 0 2 0 2 2 0 0 0 0 0 0 0 0 0 2 2 0 2 2 0 0 0 2 2 2 2 1 1 0 3 0 2 2 2 0 0 2 0 2 0 0 0 0 0 2 2 2 0 0 0 2 3 0)
>
Counts of these samples are required (when needing a function, one can just type in a likely name and use "Search in Help Desk"...); try it out:
> (count (lambda (n) (= n 0)) samples)
51
>
The weights are required as a vector, so add:
(define weights
(vector (count (lambda (n) (= n 0)) samples)
(count (lambda (n) (= n 1)) samples)
(count (lambda (n) (= n 2)) samples)
(count (lambda (n) (= n 3)) samples)))
> weights
'#(51 6 40 3)
>
(After learning Scheme/Racket basics, one can eliminate the repetition in the definition above)
And then, finally,
> (multinomial-dist 100 weights)
(multinomial-dist 100 '#(51/100 3/50 2/5 3/100))
>
The distribution you're supposed to represent is not a multinomial distribution, which is a distribution over vectors.
I think you need to use discrete-dist instead.
I have board [8,8] and I'm trying to implement the horizontal movement and vertical movement based on the movements up, down, left and right, movements of a rook on a chessboard and I am having difficulty in how to move piece to square with the number of square to move.
(defun board ()
'((64 35 74 26 21 57 12 28)
(43 15 47 53 24 56 42 29)
(51 41 71 31 17 45 55 30)
(67 66 22 T 54 75 32 38)
(13 11 16 23 25 27 33 20)
(34 36 37 44 46 52 61 48)
(10 49 59 69 68 70 50 40)
(62 63 65 72 73 76 77 58)))
The Rook moves horizontally and vertically any number of squares, forwards or backwards. In the diagram the Rook can move to any of the highlighted squares.
Function to check if the coordinates are valid
(defun position-valid (x y)
(and (>= x 0) (>= y 0) (< x 8) (< y 8)))
Function that will move the tower according to the coordinates (x, y)
(defun move-piece (x y dx dy board)
(let ((new-board (copy-tree board))
(new-x (+ x dx))
(new-y (+ y dy))
(piece (nth x (nth y board))))
(setf (nth x (nth y new-board)) nil
(nth new-x (nth new-y new-board)) piece)
new-board))
Function that moves the piece down
(defun DOWN (x y board)
(cond
((equal (position-valid (+ x 1) (+ y 0)) 'T)
(move-piece x y 1 0 board))
(T NIL)))
Function that moves the piece to up
(defun UP (x y board)
(cond
((equal (position-valid (- x 1) (+ y 0)) 'T)
(move-piece x y -1 0 board))
(T NIL)))
Function that moves the piece to the left
(defun LEFT (x y board)
(cond
((equal (position-valid (+ x 0) (- y 1)) 'T)
(move-piece x y 0 -1 board))
(T NIL)))
Function that moves the piece to the right
(defun RIGHT (x y board)
(cond
((equal (position-valid (+ x 0) (+ y 1)) 'T)
(move-piece x y 0 1 board))
(T NIL)))
now the goal is to implement the vertical and horizontal movements based on the movements mentioned above so that the piece is moved and in this case, I think that we still need to implement the possible moves based on the type of movement and how many squares to move
I implemented this list of operators for horizontal and vertical movement but it is not working
Function that moves the Tower horizontally
(defun HORIZONTAL (x y n mov board) ;;n is number of square to move
(cond
((and (equal (position-valid (+ x 0) (- y 1)) 'T) ;;left
(equal (position-valid (+ x 0) (+ y 1)) 'T));;right
(cond
((equal mov 'LEFT) (LEFT x y board))
((equal mov 'RIGHT) (RIGTH x y board))
(T (HORIZONTAL x y (1- n) mov board))))
(T NIL)))
Function that makes the Tower move in the vertical direction,
(defun VERTICAL(x y n mov board) ;;n is number of square to move
(cond
((and (equal (position-valid (- x 1) (+ y 0)) 'T) ;;up
(equal (position-valid (+ x 1) (+ y 0)) 'T));;down
(cond
((equal mov 'DOWN) (DOWN x y board))
((equal mov 'UP) (UP x y board))
(T (VERTICAL x y (1- n) mov board))))
(T NIL)))
and how to get the possible moves of the tower on the board based on the type of moves
Anny suggestion?
It seems to me that you are building too many functions which are unnecessary. What I would do is to have a MOVE function, based on move-piece, which would do both horizontal and vertical displacement. Since you have the parameter mov, which can be UP, DOWN, LEFT or RIGHT, the horizontal and vertical movements are already implicit, so there is no need to have a separate function for each direction.
So this is what I would do:
(setq board
'((64 35 74 26 21 57 12 28)
(43 15 47 53 24 56 42 29)
(51 41 71 31 17 45 55 30)
(67 66 22 T 54 75 32 38)
(13 11 16 23 25 27 33 20)
(34 36 37 44 46 52 61 48)
(10 49 59 69 68 70 50 40)
(62 63 65 72 73 76 77 58)))
(defun position-valid (x y)
(and (>= x 0) (>= y 0) (< x 8) (< y 8)) )
(defun move-piece (x y dx dy board)
(let ((new-board (copy-tree board))
(new-x (+ x dx))
(new-y (+ y dy))
(piece (nth x (nth y board))) )
(when (position-valid new-x new-y)
(setf (nth x (nth y new-board)) nil
(nth new-x (nth new-y new-board)) piece ))
new-board))
(defun MOVE (x y n mov board) ;; n is number of squares to move
(case mov
(UP (move-piece x y 0 (- n) board))
(DOWN (move-piece x y 0 n board))
(LEFT (move-piece x y (- n) 0 board))
(RIGHT (move-piece x y n 0 board))
(otherwise NIL) ))
And then, if you want to get a list of all possible moves:
(defun valid-moves (x y board)
(let (result)
(dolist (mov '(up down left right) result)
(dotimes (n 7)
(when (move x y n mov board)
(push (list n mov) result) )))))
I want to try to draw stress and strain curve for copper nanoparticles with Lammps.
This is my code.
I don't know whether this is correct or not.
Can anybody help me?
It has fix nve for relaxation, but before that has fix nvt ,and these 2 fixes can't come together.
# tensile test
echo both
dimension 3
boundary s p p
units metal
atom_style atomic
##########create box#######
region copperbox block -80 80 -40 40 -40 40 units box
create_box 1 copperbox
lattice fcc 3.61
region copper block -60 60 -20 20 -20 20 units box
create_atoms 1 region copper
mass 1 63.546
thermo_modify lost ignore
region left block -60 -50 -20 20 -20 20 units box
group left region left
region right block 50 60 -20 20 -20 20 units box
group right region right
group middle subtract all left right
timestep 0.002
pair_style eam
pair_coeff * * cu_u3.eam
velocity all create 300 4928459 mom yes rot yes dist uniform
velocity left create 0 4928459 mom yes rot yes dist uniform
velocity right create 0 4928459 mom yes rot yes dist uniform
fix 1 all nvt temp 300 300 0.01
fix 2 left setforce 0 0 0
fix 3 right setforce 0 0 0
fix 4 all nve
thermo 100
run 1000
#####################################
compute 1 middle stress/atom
compute 2 middle reduce sum c_1[1]
dump 1 all custom 100 stress.txt mass x y z c_1[1] c_1[2] c_1[3]
c_1[4] c_1[5] c_1[6]
dump 2 all xyz 100 dump.xyz
#####################################
variable a loop 2
label loopa
fix 8 right move linear 0.2 0 0 units box
fix 9 left move linear 0 0 0 units box
run 1000
#####################################
unfix 8
unfix 9
fix 8 right move linear 0 0 0 units box
fix 9 left move linear 0 0 0 units box
run 40000
#####################################
fix 10 all nve
thermo 100
run 10000
#####################################
variable sigma equal "c_2/(40000)*(10^4)"
variable l2 equal xcm(right,x)
variable l0 equal ${l2}
variable strain equal "(v_l2-v_l0)/(v_l0)*100"
next a
jump in.copper99 loopa
restart 1000 restart.*
When I do (/ 7 2), what should I do to get the result 3? If I do (/ 7 2.0), I get 3.5, which is as expected.
(floor 7 2)
Ref: http://rosettacode.org/wiki/Basic_integer_arithmetic#Common_Lisp
See FLOOR, CEILING and TRUNCATE in ANSI Common Lisp.
Examples (see the positive and negative numbers):
CL-USER 218 > (floor -5 2)
-3
1
CL-USER 219 > (ceiling -5 2)
-2
-1
CL-USER 220 > (truncate -5 2)
-2
-1
CL-USER 221 > (floor 5 2)
2
1
CL-USER 222 > (ceiling 5 2)
3
-1
CL-USER 223 > (truncate 5 2)
2
1
Usually for division to integer TRUNCATE is used.
You can use the floor function:
(floor 7 2)
3
1
Note that it returns multiple values, and you only need the first one. Since floor returns multiple values, that can be done with multiple-value-bind as follows:
(multiple-value-bind (q r) (floor 7 2) q)
=> 3
Edit: As Rainer notes in his comment, you can just pass the result of floor as an argument if all you need is the quotient.
[1]> (floor 7 2)
3 ;
1
[2]> (+ (floor 7 2) 5)
8
[3]>
I'm leaving the reference to multiple-value-bind in the answer, since it's an important function to be familiar with.
Use the floor function. In SBCL:
* (floor (/ 7 2))
3
1/2
Two values are returned, the integer part and the fractional part.