Array element reversal Implement a RISC-V assembly code snippet that reverses the elements of a 10-element integer array - riscv

`I am writing a RISC-V code that will reverse the input number. The input number is 10, the output will be the reverse of this 10 numbers.
like this
A_OUT[9] = A_IN[0]
A_OUT[8] = A_IN[1]
A_OUT[7] = A_IN[2]
but my code's reslut is like this
A_OUT[9] =
A_OUT[8] = A_IN[0]
A_OUT[7] = A_IN[1]
how can I fix my code`
.data
A_IN: .space 40
A_OUT: .space 40
PROMPT: .asciz "Enter an integer: "
.text
main:
\# Load addresses of A_IN and A_OUT into two general purpose registers
la x1, A_IN
la x2, A_OUT
\# Initialize loop index and loop bound for reading 10 integers from console
li x3, 0 # loop index
li x4, 10 # loop bound
LOOP1:
\# Print the prompt
la a0, PROMPT
li a7, 4 # ecall number for printing string
ecall
li a7, 5 # ecall number for reading integer
ecall
addi x3, x3, 1 # increment loop index
sw a0, 0(x1)
addi x1, x1, 4 # move to next location in A_IN
bne x3, x4, LOOP1
li x3, 9 # loop index
li x4, 0 # loop bound
LOOP2:
lw a0, 0(x1)
addi x1, x1, -4 # move to previous location in A_IN
sw a0, 0(x2)
addi x2, x2, 4 # move to next location in A_OUT
addi x3, x3, -1
bge x3, x4, LOOP2
li a7, 10 # ecall number for exit
ecall
As you can see, there is a 0 between two 10, how can I fix it
there is something wrong in the second loop, but i couldn't fix it please keep my style coding, just find the problems

Related

Python Iterative Scipy.Optimize.Minimize Solve for chosen inputs

I have set-up a polynomial equation with 4 factors inside, that I want to solve using scipy.optimize.minimize.
However, I might want to solve only a certain set of the 4 factors at any one time. For example,
-> given Y(x1, x2, x3, x4) = Function(x1, x2, x3, x4) = 0.0 (to get the roots)
Sometimes, I want to get all [x1, x2, x3, x4], but sometimes, I want to just solve-for/calibrate [x2, x3] etc, or any iteration of the set [x1, x2, x3, x4].
Is there a way to do this elegantly?
def fit_beta(fwd_ = 0.01, shift_ = 0.0, time_ = 1.0, beta = 1.0, k_ = np.array([]), vols_ = np.array([]), params = np.array([1, 1, 1, 1])):
def calib_(x):
curve_ = []
for strike_ in k_:
vol_ = vol_log(strike_, fwd_, time_, x[0], x[1], x[2], x[3])
curve_.append(vol_)
return np.sum((np.array(curve_) - np.array(vols_))**2)
x0 = np.array([0.01, 0.5, 0.00, 0.10]) # initial guess
bounds = [(0.0001, None), (0.0001, 0.9999), (-0.9999, 0.9999), (0.0001, None)]
res = minimize(calib_, x0, method='L-BFGS-B', bounds=bounds)
x1_, x2_, x3_, x4_ = res.x
return [x1_, x2_, x3_, x4_]

Cortex-M3 SysTick double interrupt

I'm creating a rtos system, with system tick (1ms) for real-time and trigger pendsv-which used for switch task. Priority of systick and pendsv same as 0 (group = 16; <SCB.AIRCR.PRIGROUP = 0b011>). PendSv also trigger by <SCB.ICSR.PENDSVSET = 1> when current task is sleep.
Sometime, PendSV and SysTick occurs at the same time, and SysTick is executed first, then PendSv. But after PendSV executed, SysTick occurs again.
I was tried by change priority, and clear <SCB.ICSR.PENDSTCLR = 1> in system tick handle, nothing changed.
Do you know why, and how to fix that?
logic_analyzer
asm code:
//#define USING_PUSH_POP
EXTERN GPIOA_BITBAND
EXTERN RTOS_GetCurrentCPU //CPU_TypeDef* RTOS_GetCurrentCPU();
EXTERN RTOS_GetNextCPU //CPU_TypeDef* RTOS_GetNextCPU();
PUBLIC PendSV_Handler
PUBLIC RTOS_ASM_Begin //void RTOS_ASM_ASM_Begin(CPU_TypeDef* CPU);
SECTION rtos_function:CODE
PendSV_Handler:
//PUSH in SP when interrupt
//Here - R0 - R1 - R2 - R3 - R12 - BACK_LR - BACK_PC - EAPSR
PUSH {LR}
LDR.W R0, =GPIOA_BITBAND //(1)
MOV R1, #1
STR R1, [R0, #0x180]
BL RTOS_GetCurrentCPU
CMP R0, #0
BEQ BREAK //if (NULL): back to current task
BL RTOS_ASM_SaveCurrentCPU
BL RTOS_GetNextCPU
CMP R0, #0
BEQ BREAK //if (NULL): back to current task
B RTOS_ASM_SwitchNextCPU
BREAK:
POP {PC}
//===================================
RTOS_ASM_Begin: //R0: CPU_TypeDef* MainCPU
PUSH {R1, LR}
STR LR , [R0, #28]
STR R1 , [R0, #24]
ADDS R1 , R0, #24
MOV SP , R1
BL RTOS_ASM_SaveCurrentCPU
POP {R1, PC}
//===================================
RTOS_ASM_SaveCurrentCPU: //R0: CPU_TypeDef* CurrentCPU
#ifdef USING_PUSH_POP
MOVS R1 , SP
STR SP , [R0, #(4 * 16)]
ADDS R0 , R0, #(4 * 16)
MOVS SP , R0
PUSH {R4-R11}
MOVS SP , R1
#else
STR R4 , [R0, #(4 * 8 )]
STR R5 , [R0, #(4 * 9 )]
STR R6 , [R0, #(4 * 10)]
STR R7 , [R0, #(4 * 11)]
STR R8 , [R0, #(4 * 12)]
STR R9 , [R0, #(4 * 13)]
STR R10, [R0, #(4 * 14)]
STR R11, [R0, #(4 * 15)]
STR SP , [R0, #(4 * 16)]
#endif
BX LR
//===================================
RTOS_ASM_SwitchNextCPU: //R0: CPU_TypeDef* NextCPU
#ifdef USING_PUSH_POP
ADDS R1 , R0, #(4 * 8)
MOVS SP , R1
POP {R4-R11}
LDR SP , [R0, #(4 * 16)]
#else
LDR R4 , [R0, #(4 * 8 )]
LDR R5 , [R0, #(4 * 9 )]
LDR R6 , [R0, #(4 * 10)]
LDR R7 , [R0, #(4 * 11)]
LDR R8 , [R0, #(4 * 12)]
LDR R9 , [R0, #(4 * 13)]
LDR R10, [R0, #(4 * 14)]
LDR R11, [R0, #(4 * 15)]
LDR SP , [R0, #(4 * 16)]
#endif
LDR.W R0, =GPIOA_BITBAND //(2)
MOV R1, #0
STR R1, [R0, #0x180]
POP {PC}
//===================================
END
system tick handle:
void SysTick_Handler()
{
GPIOB_BITBAND.ODR._10 ^= 1; //(3)
System.MsTick++;
if (TaskManager.Running)
RTOS_SWITCH_TRIGGER();
//SCB.ICSR.REG = BIT25;
SCB.ICSR.BITS.PENDSTCLR = 1;
}
My solution now is using SVC instead of PendSV

How to calculate difference between rows in Pandas DataFrame?

In a dataframe I have 4 variables that are the X, Y, Z and W orientations of a robot. Each line represents a measurement with these four values.
x = [-0.75853, -0.75853, -0.75853, -0.75852]
y = [-0.63435, -0.63434, -0.63435, -0.63436]
z = [-0.10488, -0.10490, -0.10492, -0.10495]
w = [-0.10597, -0.10597, -0.10597, -0.10596]
df = pd.DataFrame([x, y, z, w], columns=['x', 'y', 'z', 'w'])
I wrote the function below that returns three differences between two quaternions:
from pyquaternion import Quaternion
def quaternion_distances(w1, x1, y1, z1, w2, x2, y2, z2):
""" Create two Quaternions objects and calculate 3 distances between them """
q1 = Quaternion(w1, x1, y1, z1)
q2 = Quaternion(w2, x2, y2, z2)
dist_by_signal = Quaternion.absolute_distance(q1, q2)
dist_geodesic = Quaternion.distance(q1, q2)
dist_sim_geodec = Quaternion.sym_distance(q1, q2)
return dist_by_signal, dist_geodesic, dist_sim_geodec
This difference is calculated based on the values of the second line by the values of the first line. Thus, I cannot use the Pandas apply function.
I have already added three columns to the dataframe, so that I receive each of the values returned by the function:
df['dist_by_signal'] = 0
df['dist_geodesic'] = 0
df['dist_sim_geodec'] = 0
The problem is: how to apply the above function to each row and include the result in these new columns? Can you give me a suggestion?
Consider shift to create adjacent columns, w2, x2, y2, z2, of next row values then run rowwise apply which does require axis='columns' (not index):
df[[col+'2' for col in list('wxyz')]] = df[['x', 'y', 'z', 'w']].shift(-1)
def quaternion_distances(row):
""" Create two Quaternions objects and calculate 3 distances between them """
q1 = Quaternion(row['w'], row['x'], row['y'], row['z'])
q2 = Quaternion(row['w2'], row['x2'], row['y2'], row['z2'])
row['dist_by_signal'] = Quaternion.absolute_distance(q1, q2)
row['dist_geodesic'] = Quaternion.distance(q1, q2)
row['dist_sim_geodec'] = Quaternion.sym_distance(q1, q2)
return row
df = df.apply(quaternion_distances, axis='columns')
print(df)
You can use.
Quaternions=df.apply(lambda x: Quaternion(x), axis=1)
df['dist_by_signal'] = 0
df['dist_geodesic'] = 0
df['dist_sim_geodec'] = 0
df.reset_index(drop=True)
for i in df.index:
q1=Quaternions[i]
if i+1<len(df.index):
q2=Quaternions[i+1]
df.loc[i,['dist_by_signal','dist_geodesic','dist_sim_geodec']]=[Quaternion.absolute_distance(q1, q2), Quaternion.distance(q1, q2),Quaternion.sym_distance(q1, q2)]
print(df)
x y z w dist_by_signal dist_geodesic \
0 -0.75853 -0.75853 -0.75853 -0.75852 0.248355 0.178778
1 -0.63435 -0.63434 -0.63435 -0.63436 1.058875 1.799474
2 -0.10488 -0.10490 -0.10492 -0.10495 0.002111 0.010010
3 -0.10597 -0.10597 -0.10597 -0.10596 0.000000 0.000000
dist_sim_geodec
0 0.178778
1 1.799474
2 0.010010
3 0.000000

Unable to get the simplified coordinates using sympy - python

I have x2, x3, y2, y3, d1, d2, d3 values which is,
x2 = 0
x3 = 100
y2 = 0
y3 = 0
d1 = 100
d2 = 100
d3 = 87
When I use the below script,
from sympy import symbols, Eq, solve
x, y = symbols('x y')
eq1 = Eq((x - x2) ** 2 + (y - y2) ** 2 - d2 ** 2)
eq2 = Eq((x - x3) ** 2 + (y - y3) ** 2 - d3 ** 2)
sol_dict = solve((eq1, eq2), (x, y))
I got the ans as,
sol_dict = [(12431/200, -87*sqrt(32431)/200), (12431/200, 87*sqrt(32431)/200)]
How can I achieve the simplified solution like
sol_dict = [(62.155, -78.33), (62.155, 78.33)]
in python?
You can numerically evaluate the solution to get floats:
In [40]: [[x.evalf(3) for x in s] for s in sol_dict]
Out[40]: [[62.2, -78.3], [62.2, 78.3]]
I would only recommend doing that for display though. If you want to use the values in sol_dict for further calculations it's best to keep them as exact rational numbers.

How to "trap" my surface patches to prevent the background from bleeding through the cracks?

In response to a challenge in comp.lang.postscript, I'm working-up my 3D chops trying to render a cylinder as projected rectangular patches. But I'm still seeing the wire-frame even after I comment-out the line-drawing, because the patches don't butt-up flush.
The cylinder is modeled along the z-axis by double-looping over z (-2 .. 2, step 4/N) and theta (0 .. 360, step 360/N). The four points of the rectangle are:
v1 = (Rcos T, Rsin T, z)
v4 = (Rcos T, Rsin T, z+dz)
v2 = (Rcos (T+dT), Rsin (T+dt), z)
v3 = (Rcos (T+dT), Rsin (T+dt), z+dz)
Then we apply a model->world rotation to all four points. Then we take the vectors v1->v4 and v1->v2 and do a cross product to get the normal vector for the patch. Take a dot product with the eye vector to check if the patch is on "this side" of the shape; if not, skip the drawing and procede to the next patch (fall off the bottom of the loop). Then we apply a perspective projection to each point and draw the quadrilateral with regular postscript 2D moveto and lineto. One last calculation on the normal vector to set the graylevel and then fill.
So the question is: Is there a usual way to deal with this? Is it a 3D problem or just a numerical problem (floating-point round-off kind of stuff)? Do I just add a little fudge-factor to my dz and dT when calculating the points? Or stroke the edges explicitly? These last 2 options both produce the desired result but I can't say that I'm satisfied with them. While each make be used on an individual illustration, it doesn't really solve the problem, you know?
I took a dump of the points being used. Here's the first few from N=12.
It appears to me that, as predicted, v2 and v3 coincide precisely with v1 and v4 of the next piece on the band. These are the 2D "user coordinates" passed to moveto and lineto to produce the individual quadrilaterals. Since the CTM doesn't change, these points should map to the same pixels, right? So it does appear to be a very similar issue to the linked question. But I'm using Postscript precisely because I don't want to muck-about with writing my own rasterization routine :). I really think that the solution from the linked question, mapped to Postscript, would be to reverse the orientation of alternating checkerboard squares, at least for even N. That way, all corresponding edges are drawn in the same direction (as each other).
[-2.64550757 2.08465409]
[-3.00470281 1.69015563]
[-2.7090168 1.69015563]
[-2.38403082 2.08465409]
[-3.00470281 1.69015563]
[-3.28940701 0.936108589]
[-2.96660638 0.936108589]
[-2.7090168 1.69015563]
[-3.28940701 0.936108589]
[-3.4 -0.0666666701]
[-3.0666666 -0.0666666701]
[-2.96660638 0.936108589]
[-3.4 -0.0666666701]
[-3.28940701 -1.05890918]
[-2.96660638 -1.05890918]
[-3.0666666 -0.0666666701]
[-3.28940701 -1.05890918]
[-3.00470281 -1.78584146]
[-2.7090168 -1.78584146]
[-2.96660638 -1.05890918]
I've added a simple light model and tweaked it to bring out more mids. Jpeg output doesn't exhibit the problem, presumably due to the lossy compression. So here's a PNG snapshot.
The effect is much more apparent if I use the eye-vector as the light source. Here's xpost on the left showing the problem and gs on the right showing a modification where dz and dt are multiplied by a fudge factor of 1.06.
And the code: [Do not use this code. There is an error in the matmul routine. Corrected routines available here. Completed challenge available here.]
%!
%A shaded cylinder! Woohoo!
%(mat.ps) run
%!
%mat.ps
%Matrix and Vector math routines
/.error where { pop /signalerror { .error } def } if
/dot { % u v
2 copy length exch length ne {
/dot cvx /undefinedresult signalerror
} if
% u v
0 % u v sum
0 1 3 index length 1 sub { % u v sum i
3 index 1 index get exch % u v sum u_i i
3 index exch get % u v sum u_i v_i
mul add % u v sum
} for % u v sum
3 1 roll pop pop % sum
} bind def
% [ x1 x2 x3 ] [ y1 y2 y3 ] cross [ x2*y3-y2*x3 x3*y1-x1*y3 x1*y2-x2*y1 ]
/cross { % u v
dup length 3 ne
2 index length 3 ne or {
/cross cvx /undefinedresult signalerror
} if
% u v
exch aload pop 4 3 roll aload pop % x1 x2 x3 y1 y2 y3
[
5 index 2 index mul % ... [ x2*y3
3 index 6 index mul sub % ... [ x2*y3-y2*x3
5 index 5 index mul % ... [ x2*y3-y2*x3 x3*y1
8 index 4 index mul sub % ... [ x2*y3-y2*x3 x3*y1-x1*y3
8 index 5 index mul % ... [ x2*y3-y2*x3 x3*y1-x1*y3 x1*y2
8 index 7 index mul sub % ... [ x2*y3-y2*x3 x3*y1-x1*y3 x1*y2-x2*y1
]
7 1 roll 6 { pop } repeat
} bind def
/transpose { STATICDICT begin
/A exch def
/M A length def
/N A 0 get length def
[
0 1 N 1 sub { /n exch def
[
0 1 M 1 sub { /m exch def
A m get n get
} for
]
} for
]
end } dup 0 6 dict put def
/matmul { STATICDICT begin
/B exch def
B 0 get type /arraytype ne { /B [B] def } if
/A exch def
A 0 get type /arraytype ne { /A [A] def } if
/Q B length def
/R B 0 get length def
/P A length def
Q A 0 get length ne {
/A A transpose def
/P A length def
Q A 0 get length ne {
A B end /matmul cvx /undefinedresult signalerror
} if
} if
[
0 1 R 1 sub { /r exch def
[
0 1 P 1 sub { /p exch def
0
0 1 Q 1 sub { /q exch def
A p get q get
B q get r get mul
add
} for
} for
]
} for
]
end } dup 0 10 dict put def
%u v {operator} vop u(op)v
%apply a binary operator to corresponding elements
%in two vectors producing a third vector as result
/vop { 1 dict begin
/op exch def
2 copy length exch length ne {
/vop cvx end /undefinedresult signalerror
} if
[ 3 1 roll % [ u v
0 1 2 index length 1 sub { % [ ... u v i
3 copy exch pop get % u v i u_i
3 copy pop get % u v i u_i v_i
op exch pop % u v u_i(op)v_i
3 1 roll % u_i(op)v_i u v
} for % [ ... u v
pop pop ]
end } def
%length of a vector
/mag { 0 exch { dup mul add } forall } def
% x y z ang -> x y' z'
/rotx { 3 dict begin
/theta exch def
/z exch def
/y exch def
y theta cos mul
z theta sin mul sub
y theta sin mul
z theta cos mul add
end } def
% x y z ang -> x' y z'
/roty { 4 dict begin
/theta exch def
/z exch def
/y exch def
/x exch def
x theta cos mul
z theta sin mul add
y
x theta sin mul neg
z theta cos mul add
end } def
% x y z ang -> x' y' z
/rotz { 4 dict begin
/theta exch def
/z exch def
/y exch def
/x exch def
x theta cos mul
y theta sin mul sub
x theta sin mul
y theta cos mul add
z
end } def
% x y z -> x' y' z'
/model {
%ang roty
%ang .25 mul rotx
%alpha rotz
beta roty
gamma rotx
} def
% Eye coords
/ex .1 def
/ey .1 def
/ez 5 def
/eyedir [ex ey ez]
dup mag [ exch dup dup ]{div} vop
def
% x y z -> X Y
/project {
3 dict begin
/z exch def
/y exch def
/x exch def
1 ez z sub div
x ez mul z ex mul sub
1 index mul
y ez mul z ey mul sub
3 2 roll mul
end } def
/light
[ 3 -7 -2 1 ]
dup mag [ exch dup dup dup ]{div} vop
def
/Ia .4 def % Incident Ambient Intensity
/Ka .4 def % Ambient Diffuse reflection constant
/Il .5 def % Incident intensity of Lightsource
/Kd .3 def % Diffuse reflection constant
%h R N
/cylinder { 20 dict begin
/N exch def
/R exch def
/h exch def
/dz 1 N div def
/dt 360 dz mul def
/hdz h dz mul def
0 dz 1 dz sub {
h mul h 2 div sub /z exch def
0 dt 360 { /t exch def
/v1 [ t cos R mul
t sin R mul
z ] def
/v4 [ v1 aload pop pop
z hdz add ] def
/t t dt add def
/v2 [ t cos R mul
t sin R mul
z ] def
/v3 [ v2 aload pop pop
z hdz add ] def
[ v1 v2 v3 v4 ] {
aload 4 1 roll model 4 3 roll astore pop
} forall
/normal v4 v1 {sub} vop
v2 v1 {sub} vop
cross def
/nlen normal mag def
/normal normal [nlen nlen nlen] {div} vop def
[normal aload pop 1] [eyedir aload pop 1] dot 0 lt {
/action { moveto /action { lineto } def } def
[ v1 v2 v3 v4 ]
{ aload pop project action }
forall
closepath
% gsave
[normal aload pop 1]
light
%[ex ey ez neg 1] %"radiant"
dot
Il Kd mul mul
Ia Ka mul add
setgray
fill
% grestore
% stroke
} if
} for
} for
end } def
300 400 translate
280 dup dup moveto
dup neg dup neg lineto
dup neg dup lineto
dup neg lineto closepath .6 setgray fill
1 70 dup dup scale div setlinewidth
%/beta 0 def
%/gamma 0 def
%4 2 50 cylinder
/beta 90 def
/gamma 0 def
4 2 50 cylinder
%/beta 0 def
%/gamma 90 def
%4 2 50 cylinder
showpage
Alright, I've come up with something that sits a little easier in the gut.
6% fudging just feels to horrible to bear.
But Ken suggested that rounding could be involved. That means taking control of the rounding should gain one some measure of control over the problem. And it looks like it's true.
So I tried prepending all moveto and lineto calls with a call to prep:
/prep {
transform
%2 {
% exch
%floor
round
%ceiling
%2 mul cvi 2 div %round
%} repeat
itransform
} def
The comments show the various pieces I tried. Rounding on both device coordinates eliminated all horizontal bleed-lines and leaves very thin vertical bleeds. This seems to make sense assuming Ghostscript rasterizes by horizontal scanlines: it has an easier time with the horizontal ones with just a little help, but near-verticals are tougher.
But then I combined this with fudging. And I found that rounding just the device-y 'ordinate and fudging the patch dimensions by 2% eliminates all bleeds. It really lit up this batcave.
2% is an acceptable level of fudging, I think. (?)
Unfortunately, all the above requires tweaking when you adjust the value of N (the number of slices). The simplest fix to cover the whole surface is to stroke the edges in the same color as the fill. The only difficult point here is making sure the linewidth is appropriate for the scale. And the easy way to do that is to set them both together. For very high resolutions, this should probably be adjusted in some way to account for N.
1 70 dup dup scale div setlinewidth
Here's one of the images generated by the final program, a Steinmetz solid with coordinate axes and random colors, in a slightly skewed perspective (its right foot sticks out a little).

Resources