When defining the verb centigrade to convert its argument from a Fahrenheit to a centigrade, the code in the book 'J Primer' is:
centigrade =. 3 : 0
t1 =. y. - 32
t2 =. t1 * 5
t3 =. t2 % 9
)
However, I had the spelling error in 'y.' part.
But when I type 'y' instead of 'y.' in the definition, it works.
centigrade =. 3 : 0
t1 =. y - 32
t2 =. t1 * 5
t3 =. t2 % 9
)
Why is that? Thanks!
Originally, the J language used x. and y. for the internal arguments to an explicit verb. You can reset to allow for y. or x. if you want to use legacy code by using the foreign conjunction 9!:49
9!:49 [ 0 for not accepting y. 9!:49 [ 1 for accepting y. 9!:48 '' gives the current status of the flag. http://www.jsoftware.com/help/dictionary/dx009.htm (bottom of page)
test=: 3 : 'x. + y.'
|spelling error
| x. + y.
| ^
| test=:3 :'x. + y.'
9!:49 [ 1
test=: 3 : 'x. + y.'
9!:49 [ 0
test=: 3 : 'x. + y.'
|spelling error
| x. + y.
| ^
| test=:3 :'x. + y.'
Related
Definition of H Index used in this algorithm
Supposing a relational expression is represented as y = F(x1, x2, . . . , xn), where F returns an integer number greater than 0, and the function is to find a maximum value y satisfying the condition that there exist at least y elements whose values are not less than y. Hence, the H-index of any node i is defined as
H(i) = F(kj1 ,kj2 ,...,k jki)
where kj1, kj2, . . . , kjki represent the set of degrees of neighboring nodes of node i.
Now I want to find the H Index of the nodes of the following graphs using the algorithm given below :
Graph :
Code (Written in Python and NetworkX) :
def hindex(g, n):
nd = {}
h = 0
# print(len(list(g.neighbors(n))))
for v in g.neighbors(n):
#nd[v] = len(list(g.neighbors(v)))
nd[v] = g.degree(v)
snd = sorted(nd.values(), reverse=True)
for i in range(0,len(snd)):
h = i
if snd[i] < i:
break
#print("H index of " + str(n)+ " : " + str(h))
return h
Problem :
This algorithm is returning the wrong values of nodes 1, 5, 8 and 9
Actual Values :
Node 1 - 6 : H Index = 2
Node 7 - 9 : H Index = 1
But for Node 1 and 5 I am getting 1, and for Node 8 and 9 I am getting 0.
Any leads on where I am going wrong will be highly appreciated!
Try this:
def hindex(g, n):
sorted_neighbor_degrees = sorted((g.degree(v) for v in g.neighbors(n)), reverse=True)
h = 0
for i in range(1, len(sorted_neighbor_degrees)+1):
if sorted_neighbor_degrees[i-1] < i:
break
h = i
return h
There's no need for a nested loop; just make a decreasing list, and calculate the h-index like normal.
The reason for 'i - 1' is just that our arrays are 0-indexed, while h-index is based on rankings (i.e. the k largest values) which are 1-indexed.
From the definition of h-index: For a non-increasing function f, h(f) is max i >= 0 such that f(i) >= i. This is, equivalently, the min i >= 1 such that f(i) < i, minus 1. Here, f(i) is equal to sorted_neighbor_degrees[i - 1]. There are of course many other ways (with different time and space requirements) to calculate h.
So I'm defining a variable/function that takes two inputs and displays a series of 1's and 0's
bin 0 0 = '0'
bin 0 1 = '1'
bin 0 2 = '1'
bin 0 3 = '0'
bin 0 4 = '1'
now I want to create a duplicate of the bin variable except that at 0 3 there should be a 1 so I tried to achieve this in a new function
changeBin w z = binNew where
binNew w z = '1'
binNew x y = bin x y
yet if I do it like this it gives me a pattern match redundant warning and when I call changeBin 0 3 it gets into a loop but when I change the function to
changeBin w z = binNew where
binNew 0 3 = '1'
binNew x y = bin x y
this works but I want to do it the first way so I can change it anytime without writing a whole function but I dont know why it gives me a redundant error when I write the same just with the numbers it works
I am new to haskell bear with me thanks
any help is appreciated on what my error is on the first function
1 changeBin w z = binNew where
2 binNew w z = '1'
3 binNew x y = bin x y
The w on line 1 and the w on line 2 are different variables. w on line 2 does not become a pattern that only matches when it has the same value as the w given on line one –– instead it defines a new variable that shadows the old one. You need to explicitly compare:
changeBin w z = binNew
where
binNew w' z'
| w' == w && z' == z = '1'
| otherwise = bin x y
Please help me this. This should be simple but I am stuck.
Lets say:
a=0.9162904
b=0.8473002
b-a=-0.0689902
r=-1.21E-22
pa=1 /(1+EXP(-1*(a+(r)))) = 0.714285647
pb=1 /(1+EXP(-1*(b+(r)))) = 0.700000491
pb=pa=-0.014285155
Now let's say that only r and (b-a) is given to me.
What is the formula to calculate (pb-pa)?
You cannot calculate pb - pa from b-a and r. To see this let's call d = b-a. Then d is given (you cannot change it). Now, your equations can be written as
pa = 1 / (1 + exp(-a-r))
pb = 1 / (1 + exp(-b-r))
or
pa = 1 / (1 + exp(-r)exp(-a))
pb = 1 / (1 + exp(-r-d)exp(-a))
because b = d+a.
Now put
C = exp(-r)
D = exp(-r-d)
these two quantities are constants (i.e., they are given). However, a is not a constant so let's emphasize this by putting
x = exp(-a)
which means that x can be given any positive value. With these notations,
pa = 1 / (1 + C x)
pb = 1 / (1 + D x)
and
pb - pa = 1 / (1 + C x) - (1 / (1 + D x))
which depends on x for any values of d and r except the very particular case where C = D, which only happens if r = 0.
I would like to count the number of occurrences of an event (for example, x data value equals some number) and store these occurrences in order, while plotting a file in gnuplot. Say I have the following file:
1
0
0
0
1
1
0
Now I want to count how many times I have a 1 and store that number in variable N. Then I want to know the positions where that happens and store that information in an array pos, all of this while plotting the file. The result, for the example above, should be:
print N
3
print pos
1 5 6
I know how to achieve the counting:
N = 0
plot "data" u ($0):($1 == 1 ? (N = N+1, $1) : $1)
print N
3
Then to achieve the position recording, it would be schematically something like this:
N = 0 ; pos = ""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = pos." ".$0, $1) : $1) # This doesn't work!
print N
3
print pos
1 5 6
How can this be done in gnuplot without resorting to external bash commands?
Well, as sometimes happens writing down the question triggers an idea for an answer. I'll leave it here in case somebody finds it useful:
N=0 ; pos=""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = sprintf("%s %g", pos, $0+1), $1) : $1)
print N
3
print pos
1 5 6
Note I had to use $0+1 because position 1 is treated by gnuplot as zero.
While working on Exercise 6.5 of Ch06 in Dr. Middlebrook's D-OA method, I tried to make bode plot of the transfer function:
bodeplot[s/100+100/s*(1+10/s)] (input to wolframalpha)
in J
Somehow the J code phase plot doesn't agree with Mathematica's result, though the magnitude plot matches fine.
Anything wrong with my J code?
Af =: 4 : 0"_ 0
s=.0j1*y
'w q'=.x
f=.(s%w) + (w%s)*(1+w%q*s)
20*10^. | f
)
Pf =: 4 : 0"_ 0
s=.0j1*y
'w q'=.x
f=.(s%w) + (w%s)*(1+w%q*s)
(180%o.1)* 1{ *. f
)
load 'plot'
plot (; (100 10 Af (10 ^ ]))) 0.02*i.200
plot (; (100 10 Pf (10 ^ ]))) 0.02*i.200
To be more general, say a complex variable on the unit circle in the complex plane
z = cos x + I sin x
If we plot its phase angle, there will be a jump at 180 degree (from 180 to -180)
z_unit_circle =. ((2 o. ]) + (0j1 * (1 o.]))) # (180 %~ o.)
plot (180%o.1)*1{"1 *. z_unit_circle i.360
I think that's what happens when phase angle goes around 180 or -180 in the earlier J bode plot.
To avoid this jump, we can make use of the relationship Tan(Im(z)/Re(z)) = Tan(-180 + Im(z)/Re(z)), i.e. to turn -180 before hand.
phase_angle =. _180 + (180 % o.1) * (_3 o. %~/) # +.
Pf =: 4 : 0"_ 0
s=.0j1*y
'w q'=.x
f=.(s%w) + (w%s)*(1+w%q*s)
phase_angle f
)
plot (; (100 10 Pf (10 ^ ]))) 0.02*i.200
This is essentially the same as the answer provided by Eelvex.
However this phase_angle[z] has more jumps than Arg[z]
plot phase_angle"1 z_unit_circle i.360
So my question is how to make the correct bode plot in J. In other words, knowing the phase angle goes from 3rd quadrant into 2nd quadrant, thus -180 before hand
Don't use Arg (*.), use -180 + arctan(Im(T)/Re(T))
plot 180-~(180%o.1) * _3 o. %~/"1 +. T 0j1 * (10 ^ 3-~0.1*i.80)
(where T is your transfer function: T =: 3 : '(y%100) + (100*(1+10%y))%y')