I have this minimal code:
(progn
(calc)
(calc-hyperbolic)
(calc-eval "[8.66e10 = r * v, -7.51e6 = 0.5*v^2 - 6.67e-11*6e24/r]" 'push)
(calc-solve-for "[r,v]")
(print (calc-eval 1 'top))
(calc-quit))
And I get the generic solution
"[r = 86600000000. / (4621.24711316 - 2517.12631405 s2), v = 4621.24711316 - 2517.12631405 s2]"
In the manual I read:
"The Hyperbolic flag (H a S) [fsolve] tells the solver to report the
fully general family of solutions. It will invent variables n1, n2, …,
which represent independent arbitrary integers, and s1, s2, …, which
represent independent arbitrary signs (either +1 or -1)." ... "Note
that variables like n1 and s1 are not given any special interpretation
in Calc except by the equation solver itself. As usual, you can use
the s l (calc-let) command to obtain solutions for various actual
values of these variables."
How can I proceed programmatically to get the two solutions, aka, substitute s2 (I do not know why s2 instead of s1 ???) by +1 and -1?
Thanks.
(progn
(calc)
(calc-hyperbolic)
(calc-eval "[8.66e10 = r * v, -7.51e6 = 0.5*v^2 - 6.67e-11*6e24/r]" 'push)
(calc-solve-for "[r,v]")
(calc-eval -1 'push) ; value for variable s2
(calc-let 'var-s2) ; right name of variable s2
(print (calc-eval 1 'top))
(calc-quit))
Thank you.
Related
Question Asked
(defun a-sum(n p)
(setq sum 0)
( loop for i from n to p
do(setq sum (+ sum i))
)
(format t "~d" sum)
)
My code works for all of my Test Cases but using the loop method I keep returning a NIL right before my value. Is there any way to stop this? Or maybe an alernative method I'm meant to use when faced with a probem like this?
Here is your code formatted in a readable way, please refer to https://stackoverflow.com/help/formatting when asking question and follow the conventional ways of formatting the programming language your question is about:
(defun a-sum (n p)
(setq sum 0)
(loop for i from n to p
do (setq sum (+ sum i)))
(format t "~d" sum))
There are some problems here, notably:
you call SETQ on symbol sum, but there is no variable in scope that is declared with such a name. You introduce variables with let, for example:
(let ((sum 0))
;; here you are allowed to use SETQ
(setq sum 1))
Strictly speaking, your code is not a conforming program for Lisp, but it still works: the call to SETQ does modify the symbol-value of sum, so that's as-if you used a global variable. This is usually not a good idea since then your functions have effects that are not localized to their body, but also change the environment.
In a function body, the last expression is the value returned by the function, so here the value being returned is the result of evaluating (format ...). In the case you format to a stream, which is the case here, the return value is always NIL. That's why you have a NIL result. If you want to return sum then you need to have sum as the last expression in your function.
Generally speaking, a function should do one thing, not mix different actions together: either you compute a sum, or you print it, but try not doing both at the same time (except when debugging).
The loop construct is powerful enough to do the job with needing to use an intermediate sum, calling do (setq ...), etc. Read LOOP for black belts and you should be able to rewrite it more concisely.
The sum of consecutive numbers is a well-known formula that admits a solution without loops.
I have been resisting giving an answer, because instructors should not ask students questions like this (see below).
The question is to write a function which computes the sum of i from n to p, where n and p are integers, n and p >= 0 and p >= n (the question does not state this latter requirement, and it's easy to relax it in the answer, but let's assume it).
Well, before you write some laborious and futile loop, think a bit. Write out the sum by hand:
s = n + n+1 + ... + p
= (n + n+1 + ... + p
+ p + p-1 + ... + n)/2
= (n+p + n+p + ... + n+p)/2
And now there are (p - n + 1) terms in this sum, all of which are n+p. So
s = (p - n + 1)*(n+p)/2
Or
(defun a-sum (n p)
(/ (* (+ (- p n) 1)
(+ n p))
2))
And here's why you do this:
> (time (a-sum/mindless 0 1000000000))
Evaluation took:
6.716 seconds of real time
6.716005 seconds of total run time (6.713082 user, 0.002923 system)
100.00% CPU
0 bytes consed
500000000500000000
> (time (a-sum 0 1000000000))
Evaluation took:
0.000 seconds of real time
0.000003 seconds of total run time (0.000002 user, 0.000001 system)
100.00% CPU
0 bytes consed
500000000500000000
So here's the thing: if you are the instructor and you're reading this (which I am sure you are, because I would be) don't ask questions which have well-known closed-form solutions and expect students to write the terrible brute-force solution, because doing that is teaching people to be bad programmers, and you should not do that.
what would be a good hash code for a vehicle identification number, that is a string
of numbers and letters of the form "9X9XX99X9XX999999," where a "9" represents
a digit and an "X" represents a letter?
One reasonable approach is to hash the entire thing using a hash function suitable for strings, e.g. GCC's C++ Standard Library uses MURMUR32.
If you wanted to get more hands on, you could group all the digits to form one 11-digit number, and knowing the 6 letters can have 26 different values which is less than 2^5=32 - you could cheaply create a number from those letters (let's call them ABCDEF) by evaluating: A + B * 2^5 + C * 2^10 + D * 2^15 + E * 2^20 + F * 2^25
Then, separately hash both the 11-digit number and the number created from the letters with a decent hash function, and XOR or add the results; you'll have quite a good hash value for your VIN. I haven't personally evaluated it, but Thomas Mueller recommends and explains something ostensible suitable here:
uint64_t hash(uint64_t x) {
x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
x = x ^ (x >> 31);
return x;
}
I need to build an NFA (or DFA) to recognize the following language:
L = {w | w mod 3 = 1}.
So the way I tried it was to make an NFA to recognize numbers divisible by 3 and then just add 1 to them, but this approach is a lot harder than it seems (if not impossible ?).
I only managed to do an NFA to recognize numbers divisible by 3.
I will assume that w is to be interpreted as the decimal representation (without leading zeroes) of a nonnegative integer.
Given this, we can use Myhill-Nerode to iteratively determine the states we need:
the empty string can be followed by any string in L to get to a string in L. We'll call the equivalence class for this [e]. Note that this equivalence class corresponds to the initial state of a minimal DFA for L (if one exists). Note also that the initial state is not accepting since the empty string is not a valid decimal representation of a nonnegative integer.
the string 0 cannot be followed by anything to get a string in L; it leads to a dead state corresponding to equivalence class [0].
strings 1, 4 and 7 are in L so they must correspond to a new state. We'll call the equivalence class for these [1].
strings 2, 5 and 8 are not in L; however, not all strings in L lead them to strings in L. These must correspond to a new equivalence class we'll call [2].
strings 3, 6 and 9 are not in L; but these can be followed by anything in L to get a string in L. This is the same as the empty string, so we don't need a new equivalence class or state: the equivalence class is [e].
it can be verified that every two-digit decimal string is indistinguishable from some one-digit decimal string above. so, no new equivalence classes or states are needed.
To determine the transitions, simply append the transition symbol to the equivalence class's representative element and see what equivalence class the resulting string belongs to: that will be where the transition terminates. For instance, there is a transition from [e] to [0] on 0, from [e] to [1] on 1, etc.
Because 10 = 1 (mod 3), adding a new digit to the end of a decimal string will cause the new value modulo 3 to be the sum of the original number's value modulo 3 with the value of the new digit modulo 3:
x = a (mod 3)
y = b (mod 3)
x * 10 = x * 1 (mod 3) since 10 = 1 (mod 3)
x . y = x * 10 + y = x * 1 + y = x + y (mod 3)
Filling in the transitions is left as an exercise.
In my data there are only missing data (*) on the right side of the sequences. That means that no sequence starts with * and no sequence has any other markers after *. Despite this the PST (Probabilistic Suffix Tree) seems to predict a 90% chance of starting with a *. Here's my code:
# Load libraries
library(RCurl)
library(TraMineR)
library(PST)
# Get data
x <- getURL("https://gist.githubusercontent.com/aronlindberg/08228977353bf6dc2edb3ec121f54a29/raw/c2539d06771317c5f4c8d3a2052a73fc485a09c6/challenge_level.csv")
data <- read.csv(text = x)
# Load and transform data
data <- read.table("thread_level.csv", sep = ",", header = F, stringsAsFactors = F)
# Create sequence object
data.seq <- seqdef(data[2:nrow(data),2:ncol(data)], missing = NA, right= NA, nr = "*")
# Make a tree
S1 <- pstree(data.seq, ymin = 0.05, L = 6, lik = TRUE, with.missing = TRUE)
# Look at first state
cmine(S1, pmin = 0, state = "N3", l = 1)
This generates:
[>] context: e
EX FA I1 I2 I3 N1 N2 N3 NR
S1 0.006821066 0.01107234 0.01218274 0.01208756 0.006821066 0.002569797 0.003299492 0.001554569 0.0161802
QU TR *
S1 0.01126269 0.006440355 0.9097081
How can the probability for * be 0.9097081 at the very beginning of the sequence, meaning after context e?
Does it mean that the context can appear anywhere inside a sequence, and that e denotes an arbitrary starting point somewhere inside a sequence?
A PST is a representation of a variable length Markov model (VLMC). As a classical Markov model a VLMC is assumed to be homogeneous (or stationary) meaning that the conditional probabilities of the outcome given the context are the same at each position in the sequence. In other words, the context can appear anywhere in the sequence. Actually, the search for contexts is done by exploring the tree that is supposed to apply anywhere in the sequences.
In your example, for l=1 (l is 1 + the length of the context), you look only for 0-length context, i.e., the only possible context is the empty sequence e. Your condition pmin=0, state=N3 (have a probability greater than 0 for N3) is equivalent to no condition at all. So you get the overall probability to observe each state. Because your sequences (with the missing states) are all of the same length, you would get the same results using TraMineR with
seqmeant(data.seq, with.missing=TRUE)/max(seqlength(data.seq))
To get the distribution at the first position, you can use TraMineR and look at the first column of the table of cross-sectional distributions at the successive positions returned by
seqstatd(data.seq, with.missing=TRUE)
Hope this helps.
I have the following lines of code
arg1 = ( x<=a ).*(log(x)) + ( x>a).*(log(2*a-x));
num = sinh(arg1);
den = const + cosh(arg1);
re = num./den + const2;
re1 = ;
But re is not defined at x=0, as log blows up at 0. But re has a limiting value at 0 which is defined as const3.
I want re1 as const3 when x=0 and as re when x>0.
I tried using piecewise as
re1:= piecewise([x = 0, const3],[ x>0, re]);
But this does not work.
I get the error "Undefined function or method re1 for input arguments of type char.
How should I get the desired result?
Just use logical indexing:
re1 = re;
re1(x == 0) = const3;
and, even though it's probably "impossible", my experience tells me it's a good idea to do this as well:
re1(x >= 2*a) = const3;
You will need Symbolic Math Toolbox.
In general, if you define f(x) as a function and want to calculate the limit at x=a, you do as follow,
sym x
const3 = limit (sym (f),x,a)
This might help: http://www.mathworks.in/help/symbolic/limit.html