Related
How to merge all svg image paths into a single path ?
I'm trying to get a single path with contours of the SVG image below.
<svg width="100%" height="100%" viewBox="0 0 1105.04 782.74">
<g transform="translate(-47.24 -58.63)">
<path fill="#ff0000"
d="m 61.08,396.83 c 153.08053,-29.59584 98.70463,188.17778 101.49,254.17 -0.17,-0.05 -0.32,-0.11 -0.48,-0.17 -2.68,-1.06 -5.34,-2.18 -8,-3.38 l -1.7,-0.77 c -2.66667,-1.26 -5.33333,-2.59333 -8,-4 -0.93,-0.49 -1.86,-1 -2.78,-1.52 -14.80417,-8.29279 -28.25298,-18.80094 -39.88,-31.16 -0.34,-0.33 -0.66,-0.68 -1,-1 -1.62,-1.76 -3.21,-3.54 -4.75,-5.36 l -1.7,-2 c -1.82,-2.23 -3.58,-4.52 -5.29,-6.84 -0.25,-0.34 -0.49,-0.68 -0.75,-1 -6.954529,-9.64272 -12.728994,-20.08376 -17.2,-31.1 -0.14,-0.33 -0.26,-0.69 -0.39,-1 -1.373333,-3.53333 -2.59,-7.1 -3.65,-10.7 -0.46,-1.51 -0.9,-3 -1.28,-4.57 -0.14,-0.46 -0.26,-0.91 -0.37,-1.36 C 59.67,522.9 59,499.87 59,477 v -8 c 0,-9.25 0.11,-18.56 0.27,-27.86 0,-2.41 0.1,-4.81 0.15,-7.23 0.14,-6.14 0.34,-12.28 0.6,-18.42 0.06,-1.71 0.14,-3.41 0.23,-5.11 0,-0.98 0.05,-1.96 0.15,-2.94 0.24,-3.56 0.44,-7.09 0.68,-10.61 z" />
<path fill="#008000"
d="m 106.9,242.81 c 9.04946,-14.16423 18.93707,-27.77514 29.61,-40.76 8.27,-10.16 17,-20.06 26.06,-29.6 2.89351,66.44329 8.44745,70.36 -55.67,70.36 z" />
<path fill="#0000ff"
d="m 61.4,392.55 c 0,-0.42 0.07,-0.85 0.1,-1.27 0.09,-1.15 0.18,-2.3 0.28,-3.44 0.11,-1.34 0.23,-2.69 0.36,-4 0.13,-1.31 0.27,-2.91 0.42,-4.36 0.15,-1.45 0.29,-2.92 0.46,-4.37 0.17,-1.45 0.32,-2.9 0.5,-4.34 0.18,-1.44 0.34,-2.9 0.53,-4.34 0.15,-1.2 0.31,-2.39 0.47,-3.59 0.23,-1.69 0.47,-3.39 0.73,-5.07 0,-0.25 0.07,-0.51 0.11,-0.75 1.825189,-12.29838 4.312131,-24.48941 7.45,-36.52 3.284591,-12.62014 7.478897,-24.98582 12.55,-37 5.389652,-12.56826 11.700148,-24.72131 18.88,-36.36 129.53833,36.73328 9.66275,145.05688 -42.84,145.41 z" />
<path fill="orange"
d="m 186.8,658.23 c 1.24,0.26 2.47,0.51 3.69,0.72 1.66,0.32 3.31,0.61 5,0.86 0.32,0.05 0.65,0.1 1,0.13 0.66,0.11 1.34,0.2 2,0.29 1.67,0.23 3.33,0.42 5,0.57 0.79,0.08 1.59,0.15 2.39,0.21 l 1.93,0.15 v 0 c 1.05,0.08 2.09,0.14 3.12,0.17 0.64,0 1.28,0.06 1.94,0.08 h 1.52 1.89 2.57 1.15 c 0.86,0 1.72,0 2.57,-0.08 h 0.06 c 0.59,0 1.19,0 1.79,-0.08 0.16652,0.01 0.33348,0.01 0.5,0 0.5,0 1,0 1.48,-0.07 h 0.35 l 3.35,-0.26 c 1.1,-0.1 2.21,-0.23 3.33,-0.35 l 2.28,-0.27 c 9.27578,-1.29971 18.44848,-3.25121 27.45,-5.84 20.22,-5.64 40,-13.53 60.21,-19.31 26.13,-7.45 55,-11.11 80.25,-2 C 347.62734,488.38675 186.8,482.84379 186.8,658.23 Z M 403.57,58.64 C 379.83738,58.382876 356.16913,61.158678 333.14,66.9 278.25,80.69 229,110.6 186.8,148.8 237.84564,439.31818 403.57,340.22579 403.57,58.64 Z M 682,180.6 c -13.58,-0.48 -27.08,-4.78 -39.29,-11.42 -23.35,-12.7 -42,-32.31 -62.84,-48.86 C 536.57,85.85 482.87,65.05 427.78,59.89 359.64939,243.32902 854.7729,370.03315 682,180.6 Z m 28.19,-5 c 31.00768,35.06164 29.89,38.71166 29.89,-8.6 -10.01,1.24 -18.78,4.19 -29.85,8.56 z m -282.37,471.08 7,5.46 v 0 c 0,0 4.95,4.11 7.42,6.18 l 2.49,2.06 c 1.19,1 2.39,2 3.6,2.94 2.4,1.94667 4.82333,3.86667 7.27,5.76 1.21,1 2.45,1.9 3.68,2.84 19.52711,14.9297 40.12603,28.40272 61.63,40.31 4.82,2.66667 9.68333,5.27 14.59,7.81 3.27,1.67 6.56,3.33 9.86,4.94 4.96,2.42 9.96,4.75333 15,7 v 0 c 46.46636,20.85841 95.60112,35.17143 146,42.53 11.19333,1.65333 22.43,2.95333 33.71,3.9 C 746.72463,555.50461 427.82,372.86841 427.82,646.68 Z M 764.36,166.82 c -4.07753,60.70359 4.75692,40.96048 38.15,4.32 -12.64,-2.04 -25.35667,-3.49667 -38.15,-4.37 z M 1067,698.71 C 1018.3635,366.94214 764.82925,650.19372 764.36,780 c 19.56503,0.82193 39.16014,0.58826 58.7,-0.7 v 0 h 0.34 c 9.33333,-0.58667 18.62333,-1.44 27.87,-2.56 h 0.21 c 49.33062,-5.74122 97.56677,-18.61205 143.2,-38.21 m 132,-318.93 c -1.14,-2.69 -2.33,-5.36 -3.54,-8 -2.26,-5 -4.65,-9.86 -7.11,-14.72 -45.7806,57.32379 -15.3744,137.17216 36.22,138.28 -0.03,-39.16 -9.94,-79.16 -25.54,-115.61 z m -34.35,256.88 c 16.88,-16.18 31.14,-34.69 41.41,-55.85 12.35,-25.48 18,-53.07 18.46,-81.17 -92.7659,-14.2263 -59.87,63.92698 -59.87,137.02 z m 0,-319.92 c -0.1579,28.62312 -7.3011,36 21.48,36 -6.4981,-12.39277 -13.6584,-24.42684 -21.45,-36.05 z m -206,-162.6 c -1.58,-0.6 -3.16,-1.21 -4.75,-1.8 -16.76914,-6.19272 -33.90517,-11.34255 -51.31,-15.42 -138.49552,121.35354 31.21604,66.08 149.03,66.08 -28.93791,-19.91952 -60.13571,-36.33747 -92.94,-48.91 z m 99.06,53.17 c -402.99664,-60.82862 82.76,286.28035 82.76,76.87 -24.0977,-29.14421 -51.9113,-55.00443 -82.73,-76.92 z" />
</g>
</svg>
Just concatenate 'd' atributtes of the path elements, replace the first m with M:
<svg width="100%" height="100%" viewBox="0 0 1105.04 782.74">
<g transform="translate(-47.24 -58.63)">
<path fill="#ff0000"
d="m 61.08,396.83 c 153.08053,-29.59584 98.70463,188.17778 101.49,254.17 -0.17,-0.05 -0.32,-0.11 -0.48,-0.17 -2.68,-1.06 -5.34,-2.18 -8,-3.38 l -1.7,-0.77 c -2.66667,-1.26 -5.33333,-2.59333 -8,-4 -0.93,-0.49 -1.86,-1 -2.78,-1.52 -14.80417,-8.29279 -28.25298,-18.80094 -39.88,-31.16 -0.34,-0.33 -0.66,-0.68 -1,-1 -1.62,-1.76 -3.21,-3.54 -4.75,-5.36 l -1.7,-2 c -1.82,-2.23 -3.58,-4.52 -5.29,-6.84 -0.25,-0.34 -0.49,-0.68 -0.75,-1 -6.954529,-9.64272 -12.728994,-20.08376 -17.2,-31.1 -0.14,-0.33 -0.26,-0.69 -0.39,-1 -1.373333,-3.53333 -2.59,-7.1 -3.65,-10.7 -0.46,-1.51 -0.9,-3 -1.28,-4.57 -0.14,-0.46 -0.26,-0.91 -0.37,-1.36 C 59.67,522.9 59,499.87 59,477 v -8 c 0,-9.25 0.11,-18.56 0.27,-27.86 0,-2.41 0.1,-4.81 0.15,-7.23 0.14,-6.14 0.34,-12.28 0.6,-18.42 0.06,-1.71 0.14,-3.41 0.23,-5.11 0,-0.98 0.05,-1.96 0.15,-2.94 0.24,-3.56 0.44,-7.09 0.68,-10.61 z M 106.9,242.81 c 9.04946,-14.16423 18.93707,-27.77514 29.61,-40.76 8.27,-10.16 17,-20.06 26.06,-29.6 2.89351,66.44329 8.44745,70.36 -55.67,70.36 z M 61.4,392.55 c 0,-0.42 0.07,-0.85 0.1,-1.27 0.09,-1.15 0.18,-2.3 0.28,-3.44 0.11,-1.34 0.23,-2.69 0.36,-4 0.13,-1.31 0.27,-2.91 0.42,-4.36 0.15,-1.45 0.29,-2.92 0.46,-4.37 0.17,-1.45 0.32,-2.9 0.5,-4.34 0.18,-1.44 0.34,-2.9 0.53,-4.34 0.15,-1.2 0.31,-2.39 0.47,-3.59 0.23,-1.69 0.47,-3.39 0.73,-5.07 0,-0.25 0.07,-0.51 0.11,-0.75 1.825189,-12.29838 4.312131,-24.48941 7.45,-36.52 3.284591,-12.62014 7.478897,-24.98582 12.55,-37 5.389652,-12.56826 11.700148,-24.72131 18.88,-36.36 129.53833,36.73328 9.66275,145.05688 -42.84,145.41 M 186.8,658.23 c 1.24,0.26 2.47,0.51 3.69,0.72 1.66,0.32 3.31,0.61 5,0.86 0.32,0.05 0.65,0.1 1,0.13 0.66,0.11 1.34,0.2 2,0.29 1.67,0.23 3.33,0.42 5,0.57 0.79,0.08 1.59,0.15 2.39,0.21 l 1.93,0.15 v 0 c 1.05,0.08 2.09,0.14 3.12,0.17 0.64,0 1.28,0.06 1.94,0.08 h 1.52 1.89 2.57 1.15 c 0.86,0 1.72,0 2.57,-0.08 h 0.06 c 0.59,0 1.19,0 1.79,-0.08 0.16652,0.01 0.33348,0.01 0.5,0 0.5,0 1,0 1.48,-0.07 h 0.35 l 3.35,-0.26 c 1.1,-0.1 2.21,-0.23 3.33,-0.35 l 2.28,-0.27 c 9.27578,-1.29971 18.44848,-3.25121 27.45,-5.84 20.22,-5.64 40,-13.53 60.21,-19.31 26.13,-7.45 55,-11.11 80.25,-2 C 347.62734,488.38675 186.8,482.84379 186.8,658.23 Z M 403.57,58.64 C 379.83738,58.382876 356.16913,61.158678 333.14,66.9 278.25,80.69 229,110.6 186.8,148.8 237.84564,439.31818 403.57,340.22579 403.57,58.64 Z M 682,180.6 c -13.58,-0.48 -27.08,-4.78 -39.29,-11.42 -23.35,-12.7 -42,-32.31 -62.84,-48.86 C 536.57,85.85 482.87,65.05 427.78,59.89 359.64939,243.32902 854.7729,370.03315 682,180.6 Z m 28.19,-5 c 31.00768,35.06164 29.89,38.71166 29.89,-8.6 -10.01,1.24 -18.78,4.19 -29.85,8.56 z m -282.37,471.08 7,5.46 v 0 c 0,0 4.95,4.11 7.42,6.18 l 2.49,2.06 c 1.19,1 2.39,2 3.6,2.94 2.4,1.94667 4.82333,3.86667 7.27,5.76 1.21,1 2.45,1.9 3.68,2.84 19.52711,14.9297 40.12603,28.40272 61.63,40.31 4.82,2.66667 9.68333,5.27 14.59,7.81 3.27,1.67 6.56,3.33 9.86,4.94 4.96,2.42 9.96,4.75333 15,7 v 0 c 46.46636,20.85841 95.60112,35.17143 146,42.53 11.19333,1.65333 22.43,2.95333 33.71,3.9 C 746.72463,555.50461 427.82,372.86841 427.82,646.68 Z M 764.36,166.82 c -4.07753,60.70359 4.75692,40.96048 38.15,4.32 -12.64,-2.04 -25.35667,-3.49667 -38.15,-4.37 z M 1067,698.71 C 1018.3635,366.94214 764.82925,650.19372 764.36,780 c 19.56503,0.82193 39.16014,0.58826 58.7,-0.7 v 0 h 0.34 c 9.33333,-0.58667 18.62333,-1.44 27.87,-2.56 h 0.21 c 49.33062,-5.74122 97.56677,-18.61205 143.2,-38.21 m 132,-318.93 c -1.14,-2.69 -2.33,-5.36 -3.54,-8 -2.26,-5 -4.65,-9.86 -7.11,-14.72 -45.7806,57.32379 -15.3744,137.17216 36.22,138.28 -0.03,-39.16 -9.94,-79.16 -25.54,-115.61 z m -34.35,256.88 c 16.88,-16.18 31.14,-34.69 41.41,-55.85 12.35,-25.48 18,-53.07 18.46,-81.17 -92.7659,-14.2263 -59.87,63.92698 -59.87,137.02 z m 0,-319.92 c -0.1579,28.62312 -7.3011,36 21.48,36 -6.4981,-12.39277 -13.6584,-24.42684 -21.45,-36.05 z m -206,-162.6 c -1.58,-0.6 -3.16,-1.21 -4.75,-1.8 -16.76914,-6.19272 -33.90517,-11.34255 -51.31,-15.42 -138.49552,121.35354 31.21604,66.08 149.03,66.08 -28.93791,-19.91952 -60.13571,-36.33747 -92.94,-48.91 z m 99.06,53.17 c -402.99664,-60.82862 82.76,286.28035 82.76,76.87 -24.0977,-29.14421 -51.9113,-55.00443 -82.73,-76.92 z" />
</g>
</svg>
I need to find the intersection between a line and a sphere defined by the following equations
Line: P = P0 + tv, where P0 is the eye of a camera and v is the direction of a ray.
Sphere: (P - Pc)^T (P - Pc) = r^2, where Pc is the center of the sphere and r is the radius
How would I solve for t?
I assume that v is normalized.
The point of closest approach of the line to the center of the sphere occurs at t1:
v . (P0 + t1 v - Pc) = 0
v . (Pc - P0) = t1
At t1 the distance from the line to the center of the sphere is
h = sqrt((Pc - P0)^2 - t1^2)
The intersection with the surface of the sphere will occur (if it occurs at all) at t1 +/- td, where
td^2 + h^2 = r^2
td^2 = r^2 - h^2
= r^2 - (Pc - P0)^2 + t1^2
= r^2 - (Pc - P0)^2 + (v . (Pc - P0))^2
First change the coordinates to a system on the sphere center. P0 -> P0-Pc
Now you have P0=(x0,y0,z0) and the ray coordinates as
x = x0+t*vx
y = y0+t*vy
z = z0+t*vz
and make sure ǁvx,vy,vzǁ=1
The equation of the sphere is x^2+y^2+z^2-R^2 = 0 which is expanded as
t^2 + 2*(vx*x0+vy*y0+vz*z0)*t + (x0^2+y0^2+z0^2-R^2) = 0
t^2 + 2*B*t+C =0
B = vx*x0+vy*y0+vz*z0
C = x0^2+y0^2+z0^2-R^2
t = +√(B^2-C)/2-B
t = -√(B^2-C)/2-B
Which gives the two solutions, p=Pc+P0+v*t for the two t computed.
I'd like to identify what assembly GHC produces for a given function.
Here, for example, is some code which (should) rotate bits around in a word - it moves bit 0 to bit 12, bit 12 to 14, bit 14 back to 0 and similarly for the positions 1, 18, 13 and 6.
What's the best way to go about finding the assembly generated for rot0cw in the .S file produced by ghc -O2 -S ...?
I've read this answer, but I don't see any ..._rot0cw_closure in the assembly output.
import Data.Bits
import Data.Int (Int64)
import Text.Printf
import System.Environment
{-# INLINE maskbits #-}
-- set in word v the bits of b corresponding to the mask m
-- assume a bit in b is zero if not in the mask
maskbits v m b = (v .&. (complement m) .|. b)
{-# INLINE tb #-}
-- transfer bit i of word v to bit j of word m; assume bit j of m is 0
tb v i j m = m .|. (rotate (v .&. (bit i)) (j-i))
rot0cw :: Int64 -> Int64
rot0cw v = maskbits (maskbits v m1 b1) m2 b2
where
m1 = 0x0000005005
b1 = tb v 0 2 . tb v 2 14 . tb v 14 12 . tb v 12 0 $ 0
m2 = 0x0000002142
b2 = tb v 1 8 . tb v 8 13 . tb v 13 6 . tb v 6 1 $ 0
showBits v =
let set = [ i | i <- [0..35], testBit v i ]
in "bits set: " ++ (unwords $ map show set)
main = do
(arg0:_) <- getArgs
let v = read arg0
-- let v = 0x0000000005
let v' = rot0cw v
putStrLn $ printf "v = 0x%010x = %12d %s" v v (showBits v)
putStrLn $ printf "v' = 0x%010x = %12d %s" v' v' (showBits v')
I've read this answer, but I don't see any ..._rot0cw_closure in the assembly output.
You need to name your module. e.g. add module Main where at the beginning* to get Main_rot0cw_closure in the generated assembly.
* Strictly speaking, your module needs to export the function.
You will need to export the function rot0cw from your module, otherwise it's effectively dead code. So add something like this to the top of your module:
module Asm(rot0cw) where
The assembly it generates is rather large, if you dump the Cmm -ddump-cmm you can look at each of the proc blocks labeled with the name of the function you're interested in. The basic blocks GHC generates here roughly corresponds to a label in the resulting assembly. So for instance the entry code for rot0cw_closure maps to the following:
cB2:
if (Sp - 32 < SpLim) goto cB4;
Hp = Hp + 16;
if (Hp > HpLim) goto cB6;
I64[Sp - 16] = stg_upd_frame_info;
I64[Sp - 8] = R1;
I64[Hp - 8] = S#_con_info;
I64[Hp + 0] = 0;
I64[Sp - 24] = Hp - 7;
I64[Sp - 32] = stg_ap_p_info;
R2 = $fNumInt64_closure;
Sp = Sp - 32;
jump fromInteger_info; // [R2]
cB4: jump stg_gc_enter_1; // [R1]
cB6:
HpAlloc = 16;
goto cB4;
Which generates out to:
_cB2:
leaq -32(%rbp),%rax
cmpq %r15,%rax
jb _cB4
addq $16,%r12
cmpq 144(%r13),%r12
ja _cB6
movq $stg_upd_frame_info,-16(%rbp)
movq %rbx,-8(%rbp)
movq $S#_con_info,-8(%r12)
movq $0,0(%r12)
leaq -7(%r12),%rax
movq %rax,-24(%rbp)
movq $stg_ap_p_info,-32(%rbp)
movl $$fNumInt64_closure,%r14d
addq $-32,%rbp
jmp fromInteger_info
_cB6:
movq $16,192(%r13)
_cB4:
jmp *-16(%r13)
.size sat_info, .-sat_info
Invariably you'll get a lot of "noise" from all the the GC functions and various runtime calls, and opaque predefined closure objects. But if you really want to dig into the generated looking at the Cmm first is the way to go.
I have this implementation of the 15-puzzle game, using Prolog (Swipl). I have already implemented the A* search using Manhattan heuristic, but now I need to add hamming heuristic.
Do yo know how to implement it?
:- op(400,yfx,'#').
resolver(Estado,MovimientosSolucion) :- evaluar(Estado,0,F),
buscarSolucion([Estado#0#F#[]],S), reverse(S,MovimientosSolucion).
evaluar(Estado,Profundidad,F) :- evaluarCoste(Estado,Coste),
F is Profundidad + Coste.
buscarSolucion([Estado#_#_#MovimientosSolucion|_], MovimientosSolucion) :- solucion(Estado).
buscarSolucion([B|R],S) :- expandir(B,Sucesores),
insertarTodos(Sucesores,R,ListaAbiertos),
buscarSolucion(ListaAbiertos,S).
insertarUno(B,ListaAbiertos,ListaAbiertos) :- nodoRepetido(B,ListaAbiertos), ! .
insertarUno(B,[C|R],[B,C|R]) :- costeMenor(B,C), ! .
insertarUno(B,[B1|R],[B1|S]) :- insertarUno(B,R,S), !.
insertarUno(B,[],[B]).
insertarTodos([F|R],ListaAbiertos1,ListaAbiertos2) :- insertarUno(F,ListaAbiertos1,ListaAbiertos3),
insertarTodos(R,ListaAbiertos3,ListaAbiertos2).
insertarTodos([],ListaAbiertos,ListaAbiertos).
nodoRepetido(Estado#_#_#_, [Estado#_#_#_|_]).
costeMenor( _#_#F1#_ , _#_#F2#_ ) :- F1 < F2.
expandir(Estado#Profundidad#_#S,Sucesores) :- findall(Sucesor#Profundidad1#F#[Movimiento|S],
(Profundidad1 is Profundidad+1,
mover(Estado,Sucesor,Movimiento),
evaluar(Sucesor,Profundidad1,F)), Sucesores).
solucion(1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/0).
manhattan(A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P, Coste) :- a(A,CosteA), b(B,CosteB), c(C,CosteC), d(D, CosteD),
e(E,CosteE), f(F,CosteF), g(G,CosteG), h(H,CosteH),
i(I,CosteI), j(J,CosteJ), k(K,CosteK), l(L,CosteL),
m(M,CosteM), n(N,CosteN), o(O,CosteO), p(P,CosteP),
Coste is CosteA + CosteB + CosteC + CosteD + CosteE + CosteF + CosteG + CosteH + CosteI + CosteJ + CosteK + CosteL + CosteM + CosteN + CosteO + CosteP.
evaluarCoste(Tablero,Coste) :- hamming_distance(Tablero,Coste).
mover(TableroInicial,TableroFinal,moverArriba) :- moverArriba(TableroInicial,TableroFinal).
mover(TableroInicial,TableroFinal,moverAbajo) :- moverAbajo(TableroInicial,TableroFinal).
mover(TableroInicial,TableroFinal,moverDerecha) :- moverDerecha(TableroInicial,TableroFinal).
mover(TableroInicial,TableroFinal,moverIzquierda) :- moverIzquierda(TableroInicial,TableroFinal).
moverArriba(A/B/C/D/0/F/G/H/I/J/K/L/M/N/O/P,0/B/C/D/A/F/G/H/I/J/K/L/M/N/O/P).
moverArriba(A/B/C/D/E/0/G/H/I/J/K/L/M/N/O/P,A/0/C/D/E/B/G/H/I/J/K/L/M/N/O/P).
moverArriba(A/B/C/D/E/F/0/H/I/J/K/L/M/N/O/P,A/B/0/D/E/F/C/H/I/J/K/L/M/N/O/P).
moverArriba(A/B/C/D/E/F/G/0/I/J/K/L/M/N/O/P,A/B/C/0/E/F/G/D/I/J/K/L/M/N/O/P).
moverArriba(A/B/C/D/E/F/G/H/0/J/K/L/M/N/O/P,A/B/C/D/0/F/G/H/E/J/K/L/M/N/O/P).
moverArriba(A/B/C/D/E/F/G/H/I/0/K/L/M/N/O/P,A/B/C/D/E/0/G/H/I/F/K/L/M/N/O/P).
moverArriba(A/B/C/D/E/F/G/H/I/J/0/L/M/N/O/P,A/B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A/B/C/D/E/F/G/H/I/J/K/L/M/N/0/P,A/B/C/D/E/F/G/H/I/J/K/L/M/N/P/0).
moverIzquierda(A/0/C/D/E/F/G/H/I/J/K/L/M/N/O/P,0/A/C/D/E/F/G/H/I/J/K/L/M/N/O/P).
moverIzquierda(A/B/0/D/E/F/G/H/I/J/K/L/M/N/O/P,A/0/B/D/E/F/G/H/I/J/K/L/M/N/O/P).
moverIzquierda(A/B/C/0/E/F/G/H/I/J/K/L/M/N/O/P,A/B/0/C/E/F/G/H/I/J/K/L/M/N/O/P).
moverIzquierda(A/B/C/D/E/0/G/H/I/J/K/L/M/N/O/P,A/B/C/D/0/E/G/H/I/J/K/L/M/N/O/P).
moverIzquierda(A/B/C/D/E/F/0/H/I/J/K/L/M/N/O/P,A/B/C/D/E/0/F/H/I/J/K/L/M/N/O/P).
moverIzquierda(A/B/C/D/E/F/G/0/I/J/K/L/M/N/O/P,A/B/C/D/E/F/0/G/I/J/K/L/M/N/O/P).
moverIzquierda(A/B/C/D/E/F/G/H/I/0/K/L/M/N/O/P,A/B/C/D/E/F/G/H/0/I/K/L/M/N/O/P).
moverIzquierda(A/B/C/D/E/F/G/H/I/J/0/L/M/N/O/P,A/B/C/D/E/F/G/H/I/0/J/L/M/N/O/P).
moverIzquierda(A/B/C/D/E/F/G/H/I/J/K/0/M/N/O/P,A/B/C/D/E/F/G/H/I/J/0/K/M/N/O/P).
moverIzquierda(A/B/C/D/E/F/G/H/I/J/K/L/M/0/O/P,A/B/C/D/E/F/G/H/I/J/K/L/0/M/O/P).
moverIzquierda(A/B/C/D/E/F/G/H/I/J/K/L/M/N/0/P,A/B/C/D/E/F/G/H/I/J/K/L/M/0/N/P).
% coste en distancias de cada posicion
a(0,6). a(1,0). a(2,1). a(3,2). a(4,3). a(5,1). a(6,2). a(7,3). a(8,4). a(9,2). a(10,3). a(11,4). a(12,5). a(13,3). a(14,4). a(15,5).
b(0,5). b(1,1). b(2,0). b(3,1). b(4,2). b(5,2). b(6,1). b(7,2). b(8,3). b(9,3). b(10,2). b(11,3). b(12,4). b(13,4). b(14,3). b(15,4).
c(0,4). c(1,2). c(2,1). c(3,0). c(4,1). c(5,3). c(6,2). c(7,1). c(8,2). c(9,4). c(10,3). c(11,2). c(12,3). c(13,5). c(14,4). c(15,3).
d(0,3). d(1,3). d(2,2). d(3,1). d(4,0). d(5,4). d(6,3). d(7,2). d(8,1). d(9,5). d(10,4). d(11,3). d(12,2). d(13,6). d(14,5). d(15,4).
e(0,5). e(1,1). e(2,2). e(3,3). e(4,4). e(5,0). e(6,1). e(7,2). e(8,3). e(9,1). e(10,2). e(11,3). e(12,4). e(13,2). e(14,3). e(15,4).
f(0,4). f(1,2). f(2,1). f(3,2). f(4,3). f(5,1). f(6,0). f(7,1). f(8,2). f(9,2). f(10,1). f(11,2). f(12,3). f(13,3). f(14,2). f(15,3).
g(0,3). g(1,3). g(2,2). g(3,1). g(4,2). g(5,2). g(6,1). g(7,0). g(8,1). g(9,3). g(10,2). g(11,1). g(12,2). g(13,4). g(14,3). g(15,2).
h(0,2). h(1,4). h(2,3). h(3,2). h(4,1). h(5,3). h(6,2). h(7,1). h(8,0). h(9,4). h(10,3). h(11,2). h(12,1). h(13,5). h(14,4). h(15,3).
i(0,4). i(1,2). i(2,3). i(3,4). i(4,5). i(5,1). i(6,2). i(7,3). i(8,4). i(9,0). i(10,1). i(11,2). i(12,3). i(13,1). i(14,2). i(15,3).
j(0,3). j(1,3). j(2,2). j(3,3). j(4,4). j(5,2). j(6,1). j(7,2). j(8,3). j(9,1). j(10,0). j(11,1). j(12,2). j(13,2). j(14,1). j(15,2).
k(0,2). k(1,4). k(2,3). k(3,2). k(4,3). k(5,3). k(6,2). k(7,1). k(8,2). k(9,2). k(10,1). k(11,0). k(12,1). k(13,3). k(14,2). k(15,1).
l(0,1). l(1,5). l(2,4). l(3,3). l(4,2). l(5,4). l(6,3). l(7,2). l(8,1). l(9,3). l(10,2). l(11,1). l(12,0). l(13,4). l(14,3). l(15,2).
m(0,3). m(1,3). m(2,4). m(3,5). m(4,6). m(5,2). m(6,3). m(7,4). m(8,5). m(9,1). m(10,2). m(11,3). m(12,4). m(13,0). m(14,1). m(15,2).
n(0,2). n(1,4). n(2,3). n(3,4). n(4,5). n(5,3). n(6,2). n(7,3). n(8,4). n(9,2). n(10,1). n(11,2). n(12,3). n(13,1). n(14,0). n(15,1).
o(0,1). o(1,5). o(2,4). o(3,3). o(4,4). o(5,4). o(6,3). o(7,2). o(8,3). o(9,3). o(10,2). o(11,1). o(12,2). o(13,2). o(14,1). o(15,0).
p(0,0). p(1,6). p(2,5). p(3,4). p(4,3). p(5,5). p(6,4). p(7,3). p(8,2). p(9,4). p(10,3). p(11,2). p(12,1). p(13,3). p(14,2). p(15,1).
Thanks a lot
Here is the solver for 8-puzzle, extended... maybe it will use too much memory. It implements simply a greedy heuristic. Could be interesting to extend it with A*...
/* File: fifteen_puzzle.pl
Author: Carlo,,,
Created: Jul 9 2014
Purpose: solve 15-puzzle
*/
:- module(fifteen_puzzle,
[fifteen_puzzle/3
]).
:- use_module(library(nb_set)).
:- use_module(library(plunit)).
%% fifteen_puzzle(+Target, +Start, -Moves) is nondet.
%
% public interface to solver
%
fifteen_puzzle(Target, Start, Moves) :-
empty_nb_set(E),
solve(E, Target, Start, Moves).
%% -- private here --
solve(_, Target, Target, []) :-
!.
solve(S, Target, Current, [Move|Ms]) :-
add_to_seen(S, Current),
setof(Dist-M-Update,
( get_move(Current, P, M),
apply_move(Current, P, M, Update),
distance(Target, Update, Dist)
), Moves),
member(_-Move-U, Moves),
solve(S, Target, U, Ms).
%% get_move(+Board, +P, -Q) is semidet
%
% based only on coords, get next empty cell
%
get_move(Board, P, Q) :-
nth0(P, Board, 0),
coord(P, R, C),
( R < 3, Q is P + 4
; R > 0, Q is P - 4
; C < 3, Q is P + 1
; C > 0, Q is P - 1
).
%% apply_move(+Current, +P, +M, -Update)
%
% swap elements at position P and M
%
apply_move(Current, P, M, Update) :-
assertion(nth0(P, Current, 0)), % constrain to this application usage
( P > M -> (F,S) = (M,P) ; (F,S) = (P,M) ),
nth0(S, Current, Sv, A),
nth0(F, A, Fv, B),
nth0(F, C, Sv, B),
nth0(S, Update, Fv, C).
%% coord(+P, -R, -C)
%
% from linear index to row, col
% size fixed to 4*4
%
coord(P, R, C) :-
R is P // 4,
C is P mod 4.
%% distance(+Current, +Target, -Dist)
%
% compute Manatthan distance between equals values
%
distance(Current, Target, Dist) :-
aggregate_all(sum(D),
( nth0(P, Current, N), coord(P, Rp, Cp),
nth0(Q, Target, N), coord(Q, Rq, Cq),
D is abs(Rp - Rq) + abs(Cp - Cq)
), Dist).
%% add_to_seen(+S, +Current)
%
% fail if already in, else store
%
add_to_seen(S, L) :-
%term_to_atom(L, A),
findall(C, (nth0(I, L, D), C is D*10^I), Cs),
sum_list(Cs, A),
add_nb_set(A, S, true).
:- begin_tests(fifteen_puzzle).
show_square(R) :-
findall(Row, (between(1,4,_), length(Row, 4)), Rows),
append(Rows, R),
nl, maplist(show_row, Rows).
show_row(R) :-
format('~t~d~3+~t~d~3+~t~d~3+~t~d~3+~n', R).
show_solution(P, []) :-
show_square(P).
show_solution(P, [M|Ms]) :-
show_square(P),
nth0(C, P, 0),
apply_move(P, C, M, U),
show_solution(U, Ms).
target( [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]).
start(0, [1,2,3,4,5,6,7,8,9,10,11,12,0,13,14,15]).
start(1, [1,2,3,4,5,6,7,8,0,10,11,12,9,13,14,15]).
test(0) :- runtest(0).
test(1) :- runtest(1).
runtest(N) :-
target(T),
start(N, S),
fifteen_puzzle(T, S, R),
format('solution of ~d: ~w~n', [N, R]),
show_solution(S, R).
:- end_tests(fifteen_puzzle).
you can use ?- run_tests(fifteen_puzzle). for a simple test.
i try make chord piano detection by using matlab language. i able to identify every single tone note and i want to display the result by using string comparison so i make code like this
function akor=readChord(notes) %notes is input String data type
a1={'A' 'C#/Db' 'E'};
b1={'B' 'D' 'F#/Gb'};
c1={'C' 'E' 'G'};
d1={'D' 'F#/Gb' 'A'};
e1={'E' 'G#/Ab' 'B'};
f1={'F' 'A' 'C'};
g1={'G' 'A#/Bb' 'D'};
%Chord Minor
a2={'A' 'C' 'E'};
b2={'B' 'D' 'F#/Gb'};
c2={'C' 'D#/Eb' 'G'};
d2={'D' 'F' 'A'};
e2={'E' 'G' 'B'};
f2={'F' 'G#/Ab' 'C'};
g2={'G' 'A#/Bb' 'D'};
a=notes(3,:)
if v=isempty(setxor(a,a1))==1;
display('Chord A mayor')
elseif v = isempty(setxor(a,b1))==1;
display('Chord B')
elseif v = isempty(setxor(a,c1))==1;
display('Chord C mayor')
elseif v = isempty(setxor(a,d1))==1;
display('Chord D mayor')
elseif v = isempty(setxor(a,e1))==1;
display('Chord E mayor')
elseif v = isempty(setxor(a,f1)==1;
display('Chord F mayor')
elseif v = v = isempty(setxor(a,g1)==1;
display('Chord G mayor')
elseif v = isempty(setxor(a,a2))==1;
display('Chord A minor')
elseif v = isempty(setxor(a,b2))==1;
display('Chord B')
elseif v = isempty(setxor(a,c2))==1;
display('Chord C minor')
elseif v = isempty(setxor(a,d2))==1;
display('Chord D minor')
elseif v = isempty(setxor(a,e2))==1;
display('Chord E minor')
elseif v = isempty(setxor(a,f2)==1;
display('Chord F minor')
elseif v = isempty(setxor(a,g2)==1;
display('Chord G minor')
else
display('not Detect')
end
but it look like i have error
Error: File: bacagambar.m Line: 18 Column: 9
The expression to the left of the equals sign is not a valid target for an assignment.
anyone know what i should did
Using a switch staement might be more appropriate in this case
switch note(3,:)
case {'A' 'C#/Db' 'E'}
akor = 'Chord A mayor';
case {'B' 'D' 'F#/Gb'}
akor = 'Chord B';
% put all other patters in similar case
otherwise
akor = '';
error('not detected');
end
fprintf( 1, '%s\n', akor );