How can I change the position of a morph in smalltalk? 2D Grid - smalltalk

Hello I have trouble changing the position of some morphs. While it is possible to move them from the Inspector with:
self position: 50 # 50
for example.
I wrote a function wich should set the position of of a 2d collection of morphs.
Cell is a subclass of simple switchmorph. And the class owning this function is a subclass of bordered morph.
setCells
| xPos yPos row col |
xPos := 0.
yPos := 0.
row := 1.
col := 1.
cells := OrderedCollection new.
cols timesRepeat: [ cells add: OrderedCollection new ].
cells do: [ :each | rows timesRepeat: [ each add: (Cell new size: cellSize) ] ].
rows
timesRepeat: [ cols
timesRepeat: [ ((cells at: row) at: col) position: xPos # yPos.
xPos + cellSize.
row + 1 ].
row:=1.
yPos + cellSize.
col + 1 ].
cells do: [ :x | x do: [ :y | self addMorph: y ] ]
I dont get an error and actually all cells are added but all on the same position.
When I try to cast them into the world instead the same happens. All on the the same spot.
I hope someone can help me out here.
Cheers
Solution:
the solution
calculatePositions
| row col xPos yPos |
row := 1.
col := 1.
xPos := 0.
yPos := 0.
rows
timesRepeat: [ cols
timesRepeat: [ ((cells at: row) at: col) position: xPos # yPos.
xPos := xPos + cellSize.
row := row + 1 ].
row := 1.
xPos := 0.
yPos := yPos + cellSize.
col := col + 1 ]

You are not updating the variables xPos, row, yPos and col. So, instead of
xPos + cellSize.
row + 1 ].
and
row:=1.
yPos + cellSize.
col + 1].
you should say
xPos := xPos + cellSize.
row := row + 1].
and
row := 1.
yPos := yPos + cellSize.
col := col + 1].

Related

Unexplained Pinescript error shows when trying to parametrize a variable

This is the working version of the script:
//#version=5
indicator("compare MA", overlay= true)
var finallength = 0
Length1MA = input.int(defval = 10, title= "1 MA length")
Length2MA = input.int(defval = 20, title= "2 MA length")
sma1 = ta.sma(close, Length1MA)
sma2 = ta.sma(close, Length2MA)
var count1 = 0.0
var count2 = 0.0
for i = 1 to 10
if not na(sma1[i]) and not na(sma2[i])
count1 := count1 + math.abs(close[i] - sma1[i])
count2 := count2 + math.abs(close[i] - sma2[i])
if count1 > count2
finallength := Length1MA
else
finallength := Length2MA
plot(ta.sma(close, finallength), color= color.red, linewidth=2)
This is a different version that tries to parametrize the amount of Moving Averages, however there is an error i've been stuck to for a long time:
//#version=5
indicator("compare MA", overlay= true)
var finallength = 0
LengthsMA = input(defval=[10,20,30,40], title="MA lengths", type=input.integer)
counts = 0.0
for i = 1 to bar_index
for j = 0 to len(LengthsMA)-1
sma = ta.sma(close, LengthsMA[j])
if not na(sma[i])
counts[j] := counts[j] + math.abs(close[i] - sma[i])
finallength := LengthsMA[highest(counts)]
plot(ta.sma(close, finallength), color= color.red, linewidth=2)
you can run it to see the error first hand at line 12:
counts[j] = counts[j] + math.abs(close[i] - sma[i]) -> gives error -> Syntax error at input '=' (Error at 12:23)
changing it to := will not fix it! the goal is to use many Moving Averages and iterate through them all to find which is furthest from the price for the most time.
The goal is to use as many Moving Averages as needed without writing lines individually and cluttering the code with 20 else if statements, and iterate through them all to find which is furthest from the price.
the problem is this unexplainable error:
Syntax error at input '=' (Error at 12:23)
Unlike some other languages, [] in pinescript is not used for arrays. It is called history reference operator [] and is used to access historical data.
Obviously, you cannot re-write the history :)
So, doing something like
counts[j] := counts[j] + math.abs(close[i] - sma[i])
is not allowed.
If you want to work with arrays, you should use arrays.

found '0' definitions of operator "**", cannot determine exact overloaded matching definition for "**" in VHDL

I am trying to simulate the below code. However, it shows the error " found '0' definitions of operator "", cannot determine exact overloaded matching definition for "" ". I have had attached the required packages to my code. please guide me with this error. Thank you in advance.
library IEEE;
USE IEEE.std_logic_1164.all;
USE IEEE.numeric_std.all;
USE IEEE.std_logic_arith.all;
USE IEEE.math_real.all;
library IEEE_proposed;
use IEEE_proposed.fixed_pkg.all;
entity test_zp is
end;
Architecture ARCHofZP of test_zp is
TYPE matrix IS ARRAY ( NATURAL RANGE <>, NATURAL RANGE <> ) OF INTEGER RANGE 0 TO 500;
SIGNAL kernel : matrix ( 1 TO 3 , 1 TO 3 ) := ((1,0,0),(0,1,0),(1,0,1));
SIGNAL image1 : matrix ( 1 To 6 , 1 TO 6 ) := ((25,100,75,49,130,15),(50,80,0,70,100,34),(20,5,10,20,30,0),(45,60,50,12,24,32),(34,37,53,55,21,90),(45,65,55,78,20,16));
FUNCTION batchnorm (outimg : matrix ( 1 To 8 , 1 TO 8 ) ) RETURN matrix IS
VARIABLE sum1 : INTEGER RANGE 0 TO 500;
VARIABLE SD1,SD,mean,ave,sum : UFIXED (10 downto -2):= "00000000";
VARIABLE batchnorm_mat : matrix (outimg'RANGE(1), outimg'RANGE(2));
CONSTANT alpha,beta : UFIXED (1 downto -2):= "0010";
BEGIN
R1 : FOR i IN outimg'RANGE(1) LOOP
C1 : FOR j IN outimg'RANGE(2) LOOP
sum1 := sum1 + outimg(i,j);
END LOOP;
END LOOP;
mean := sum1 / outimg'LENGTH;
ave := to_ufixed(23,10,-2);
R2 : FOR i IN outimg'RANGE(1) LOOP
C2 : FOR j IN outimg'RANGE(2) LOOP
SD1 := ((to_ufixed(outimg(i,j),10,-2)-ave)/to_ufixed(outimg'LENGTH,10,-2)) + SD1;
END LOOP;
END LOOP;
SD := SD1 ** (2);
R3 : FOR i IN batchnorm_mat'RANGE(1) LOOP
C3 : FOR j IN batchnorm_mat'RANGE(2) LOOP
batchnorm_mat(i,j) := ((to_ufixed(outimg(i,j),10,-2) - ave)/(SD)) * alpha + beta ;
END LOOP;
END LOOP;
RETURN batchnorm_mat; -- return batch normalized outimg matrix named batchnorm_mat
END FUNCTION batchnorm;
begin
outimg <= zero_padding( kernel , image1 );
end Architecture ARCHofZP;

Time complexity of this program

I studied how to find the time complexity of a program. But the way I can understand is only the order. What I mean is I can only distinguish the big O of n^3, n^2, log(n), n, or 1. But I could never understand what O(V+E) or O(N*M) means. I agree that there is lacking in my fundamentals. That's why I want to learn about it. Please provide me some links or websites if you can't explain it here.
One more thing, can anyone give me any basic idea how to find the time complexity of this program?
[nRow, nCol, noOfRotation] = [int(x) for x in input().split()]
matrix = [ [0 for j in range(nCol)] for i in range(nRow) ]
for i in range(nRow):
matrix[i] = [int(x) for x in input().split()]
maxRow = nRow-1
maxCol = nCol-1
start = 0
while start < maxRow and start < maxCol:
tempMat = []
for col in range(start, maxCol):
tempMat.append( matrix[ start ][ col ] )
for row in range(start, maxRow):
tempMat.append( matrix[ row ][ maxCol ] )
for col in range(maxCol, start, -1):
tempMat.append( matrix[ maxRow ][ col ] )
for row in range(maxRow, start, -1):
tempMat.append( matrix[ row ][ start ] )
length = len(tempMat)
i = 0
for col in range(start, maxCol):
matrix[ start ][ col ] = tempMat[ (i + noOfRotation) % length ]
i += 1
for row in range(start, maxRow):
matrix[ row ][ maxCol ] = tempMat[ (i + noOfRotation) % length ]
i += 1
for col in range(maxCol, start, -1):
matrix[ maxRow ][ col ] = tempMat[ (i + noOfRotation) % length ]
i += 1
for row in range(maxRow, start, -1):
matrix[ row ][ start ] = tempMat[ (i + noOfRotation) % length ]
i += 1
start += 1
maxRow -= 1
maxCol -= 1
for i in range(nRow):
for j in range(nCol):
print(matrix[i][j], end = ' ')
print('')
Problem link: Matrix Layer Rotation

Bernoulli-number method wrong for input > 1

I'm trying to implement a method that returns the n:th bernoulli number, like so:
Object subclass: #Bernoulli.
Bernoulli class extend [
"******************************************************
* Psuedo code for bernoulli method I'm working from:
*
* function B(n)
* B[1] <-- 1
* for m <-- 2 to n+1 do
* B[m] <-- 0
* for k <-- 1 to m - 1 do
* B[m] <-- B[m] โˆ’ BINOM (m, k-1) * B[k]
* B[m] <-- B[m]/m
* return B[n+1]
*
* function BINOM (n, k)
* r <-- 1
* for i <-- 1 to k do
* r <-- r ยท (n โˆ’ i + 1)/i
* return r
******************************************************"
bernoulli: n [
"Initialize variables"
| B size innerLoop temp|
size := n + 1.
B := Array new: size.
B at: 1 put: 1. "B[1] <-- 1"
2 to: size do: [:m | "for m <-- 2 to (n+1) do"
B at: m put: 0. "B[m] <-- 0"
innerLoop := m - 1.
1 to: innerLoop do: [:k | "for k <-- 1 to (m-1) do"
B at: m put: (B at: m) - (Bernoulli binom: m k: (k-1)) * (B at: k). "B[m] <-- B[m] โˆ’ BINOM(m, k-1) * B[k]"
].
B at: m put: (B at: m) / m. "B[m] <-- B[m] / m"
].
^(B at: size) "return B[n+1]"
]
binom: n k:k [
| r i |
r := 1. "r <-- 1"
1 to: k do: [:i | "for i <-- 1 to k do"
r := r * (n - i + 1) / i. "r <-- r * (n - i + 1)/i"
].
^r "return r"
]
]
z := Bernoulli bernoulli: 3.
z printNl.
(I've done my best to comment the code).
However, for inputs n > 1, I get the wrong bernoulli number:
n = 0 --> 1 (correct).
n = 1 --> -1/2 (correct)
n = 2 --> 2/3
(should be 1/6)
n = 3 --> -7/12 (should be 0)
n = 4 --> 77/45 (should be -1/30)
n = 5 --> 3157/9720 (should be 0)
My guess is that I've implemented the inner loop or inner-inner loop wrong somehow, since input n < 2 works correctly (and n < 2 skips the inner-inner loop entirely). The psuedo code I'm working from could also be incorrect, but I doubt it since I made it work in COBOL just yesterday. The binom method works correctly, I've tested this myself.
Even so, I can't figure out why this isn't working as it should. Any help is appreciated.
Here is a translation of the original algorithm. The receiver is your parameter n and the answer is the array of all Bernoulli numbers from 0 to n.
Integer >> bernoulliNumbers
| bernoulli |
bernoulli := Array new: self + 1.
bernoulli at: 1 put: 1.
2 to: self + 1 do: [:m |
bernoulli at: m put: 0.
1 to: m - 1 do: [:k |
bernoulli
at: m
put: (bernoulli at: m) - ((m binom: k - 1) * (bernoulli at: k))].
bernoulli at: m put: (bernoulli at: m) / m].
^bernoulli
Example:
5 bernoulliNumbers -> #(1 -1/2 1/6 0 -1/30 0)
EDIT
Here is the binom: method that I created in the Integer class
binom: k
| r |
r := 1.
1 to: k do: [:i | r := r * (self - i + 1) / i].
^r
You (me) are missing parentheses around the (Bernoulli binom: m k: (k-1)) * (B at: k) clause. It should look like this:
B at: m put: (B at: m) - ((Bernoulli binom: m k: (k-1)) * (B at: k)).

Calculating matrix for positioning controls

I have to create 39 labels dynamically and place it to Panel1 in form of matrix of 3 columns and 12 rows, like this:
Label1 Label2 Label3
Label4 Label5 Label6
etc... to...
Label37 Label38 Label39
For doing this I have to calculate relative position of each label in "grid" from index.
This is my code:
Dim posX As Integer = 0
Dim posY As Integer = 0
Const offset As Integer = 25
Dim labels(39) As Label
For t As Integer = 1 To 39
labels(t) = New Label()
labels(t).Name = "Label" + t.ToString
labels(t).Text = t.ToString
labels(t).ForeColor = Color.Red
labels(t).BorderStyle = BorderStyle.FixedSingle
labels(t).AutoSize = True
posX = t Mod 3
If posX = 0 Then posX = 3
posY = CInt((t / 3) Mod 12)
labels(t).Location = New Point( _
(posX * offset) - offset, _
(posY * offset) - offset)
Debug.Print("t=" + t.ToString + " x=" + posX.ToString + " y=" + posY.ToString)
Panel1.Controls.Add(labels(t))
Next t
Wanted result of calculation "column" and "row" from which I can calculate control's position will be:
1-1, 2-1, 3-1
1-2, 2-2, 3-2
etc... to...
1-12, 2-12, 3-12
According to my code posX is calculated well (123123123123...) but posY is not and should be 111222333...
Please help to get a calculation of posY properly.
It looks to me that you probably want...
Int ((t - 1) / 3) + 1
for your Y-coordinate calculation; I don't know what the Mod 12 part is for but you I expect you could fit that in if required.
Edit - Int() is required, not CInt(), as Int rounds down, but CInt rounds to the nearest.