Difficulties understanding variable reassignment in Lisp - variables

I'm facing some problems understanding the results of a few experiments with nconc.
(setf x '(a b c))
(setf y '(1 2 3))
(nconc x y) ; => x = (A B C 1 2 3), y = (1 2 3)
From what I've read, nconc changes the rest field of x to point to y.
(setf (nth 1 y) 10) ; => x = (A B C 1 10 3), y = (1 10 3)
So far, so good.
(setf y '(4 5 6)) ; => x = (A B C 1 10 3) y = (4 5 6)
Why does x still reference to the old cons cell, or in other words does the reassignment of y not just change the data at the address of y?
Thanks in advance
Michael

Lisp variables don't point to fixed memory. They point to Lisp data objects. Setting a variable does not change any object memory. The variable just points to some other data.

Because the last cons in x is set to pointing to the cons that y was pointing at. It doesn't point at y's value dynamically or by reference.
(setf x '(a b c))
; x = (a b c)
(setf y '(1 2 3))
; x = (a b c)
; y = (1 2 3)
(nconc x y)
; x = (a b c 1 2 3)
; y = (1 2 3) = (nthcdr 3 x)
(setf (nth 1 y) 10)
; x = (a b c 1 10 3)
; y = (1 10 3) = (nthcdr 3 x)
(setf y '(4 5 6))
; x = (a b c 1 10 3)
; y = (4 5 6)

nconc changes the rest field of x to point to the value of y. The value y is the cell, which y points to. If you move the y pointer to another target, the rest field of x won't change, and will still point to (1 2 3).

Related

df.apply(myfunc, axis=1) results in error but df.groupby(df.index).apply(myfunc) does not

I have a dataframe that looks like this:
a b c
0 x x x
1 y y y
2 z z z
I would like to apply a function to each row of dataframe. That function then creates a new dataframe with multiple rows from each input row and returns it. Here is my_func:
def my_func(df):
dup_num = int(df.c - df.a)
if isinstance(df, pd.Series):
df_expanded = pd.concat([pd.DataFrame(df).transpose()]*dup_num,
ignore_index=True)
else:
df_expanded = pd.concat([pd.DataFrame(df)]*dup_num,
ignore_index=True)
return df_expanded
The final dataframe will look like something like this:
a b c
0 x x x
1 x x x
2 y y y
3 y y y
4 y y y
5 z z z
6 z z z
So I did:
df_expanded = df.apply(my_func, axis=1)
I inserted breakpoints inside the function and for each row, the created dataframe from my_func is correct. However, at the end, when the last row returns, I get an error stating that:
ValueError: cannot copy sequence with size XX to array axis with dimension YY
As if apply is trying to return a Series not a group of dataFrames that the function created.
So instead of df.apply I did:
df_expanded = df.groupby(df.index).apply(my_func)
Which just creates groups of single rows and applies the same function. This on the other hand works.
Why?
Perhaps we can take advantage of how pd.Series.explode and pd.Series.apply(pd.Series) work to simplify this process.
Given:
a b c
0 1 1 4
1 2 2 4
2 3 3 4
Doing:
new_df = (df.apply(lambda x: [x.tolist()]*(x.c-x.a), axis=1)
.explode(ignore_index=True)
.apply(pd.Series))
new_df.columns = df.columns
print(new_df)
Output:
a b c
0 1 1 4
1 1 1 4
2 1 1 4
3 2 2 4
4 2 2 4
5 3 3 4

How can I map a list of values to a dataframe

i'm trying to map some values to a dataframe, i used some looping methods but it seems that there must be a simple way to acheive the result im looking for
input_df :
A B C
x y z
i j t
f g h
list if values to map :
list = [1,2,3]
result_df :
A B C D
x y z 1
i j t 1
f g h 1
x y z 2
i j t 2
f g h 2
x y z 3
i j t 3
f g h 3
Try a cross join (i.e. Cartesian product):
tmp = pd.DataFrame(list, columns=["D"])
tmp.merge(input_df, how="cross")
Require pandas >= 1.2.0. pd.merge

Group records from Datatable by Linq and add them to a Dictionary

I have a datatable which is filled from sql query. Sample Query:
A B C D E
1 1 x y z
1 1 x y z
1 2 x y z
2 1 x y z
i want to group them like this one: (A and B will be unique)
A B C D E
1 1 x y z
1 1 x y z
A B C D E
1 2 x y z
A B C D E
2 1 x y z
i tried with linq but could not properly done it. i checked similar questions but they did not solve my problem. It does not have to be linq but i thought if i use linq and group them by using Dictionary that would be good solution for me.

differential equation by maxima

I am new in maxima, so I am really sorry if I ask simple question. I have a differential equation,
(%i1) -(x-x/2*sinh(x/2)+'diff(y,x))*(1/y+'diff(y,x)*x/y^2)+(x-x^2/sinh(x/2)+x^2*cosh(x/2)/(4*(sinh(x/2))^2)+'diff(y,x)*x+'diff(y,x,2)*x^2)/y+y^2-1-0.9*(x-x^2/(2*sinh(x/2)))=0;
2 x
2 2 x cosh(-)
2 d y dy x 2
x --- + x -- - ------- + ---------- + x
2 dx x 2 x
dx sinh(-) 4 sinh (-)
2 2
(%o1) ----------------------------------------
y
x dy
x sinh(-) x -- 2
dy 2 dx 1 2 x
+ (- -- + --------- - x) (---- + -) + y - 0.9 (x - ---------) - 1 = 0
dx 2 2 y x
y 2 sinh(-)
2
(%i2) ode2(%,y,x);
rat: replaced -0.9 by -9/10 = -0.9
(%o2) false
what should I do?
The equation you have is nonlinear. Maxima's ode2 can only solve a limited variety of differential equations, and it appears your equation doesn't fall into any of the categories it can handle.
I don't know if there is another symbolic diff eq solver in Maxima that you can try. If a numerical solution is enough, take a look at rk (a Runge-Kutta implementation).

Currying a function with a variable in J

I can create a function that multiplies by 2 with 2&\*, and indeed 20 = (2&\*)10
What I want to do is create a factory-function that makes these to order.
So, I want a monad f s.t. ( f y ) x == (y * x )
whilst (\*& 2) 3 works ((\*&) 2) 3 doesn't, so trying explicitly:
(3 : 'y&*') 2 produces a syntax error.
Where am I going wrong ?
A verb that creates a verb is actually an adverb1 in J:
f =: 1 : 'm&*'
2 f
2&*
(2 f) 5
10
(i.10)f 5
0 5 10 15 20 25 30 35 40 45
or tacitly:
f =: &*
2 f
2&*
h =: 3 :'...' won't work because that produces a verb and then h y wants to be a noun.
g =: 4 :'x&* y' is fine and equivalent to f.
[1]: or a conjunction