I'm learning Racket by writing an SDL application but I don't know how to initialize a rectangle structure. It's defined in racket-sdl as follows:
(define-cstruct _SDL_Rect
([x _int]
[y _int]
[w _int]
[h _int]))
How do I create an instance of a rectangle? Specifically, I want to create a rectangle to pass into the following function as the last parameter:
(SDL_BlitSurface hello-world-surface #f screen-surface #f)
First of all, if you are just starting out with Racket, I would recommend to use some of the packaged libraries for drawing, such as the GUI library's canvas or the OpenGL library.
The racket-sdl project, with only 3 commits (the last one being 2 years old), seems to me as no more than a feasibility test.
Still, your question is valid, so let's give you an answer. (define-cstruct ...) defines a C struct essentially as a pointer, so you have no obvious means to change its internals. You can create a small wrapper library in C with a function make_SDL_Rect, and use that, but it isn't worth the hassle. It would be better to define the SDL_Rect type using make-cstruct-type, which allows for the conversion of parameters.
See more info at the FFI manual.
Related
I'm browsing the swift tensorflow code, and stumbled upon instances of
var result = #tfop("Mul", a, b)
#tfop is well explained in the doc here, in the sense of 'what it does' but I'm also interested in what is actually is from a language standpoint, or as a function implementation.
What does #tfop represent, beside a handle to the computation graph? why the '#'? Where can I find tfop implementation if I want to? (I browsed the code, but no luck, although I can't guarantee that I didn't miss anything).
per Chris Lattner:
#tfop is a “well known” representation used for tensor operations.
It is an internal implementation detail of our stack that isn’t meant
to be user visible, and is likely to change over time.
In Swift, "#foo(bar: 42)” is the general syntax used for “macro like”
and “compiler magic” operations. For example C things like FILE
are spelled as #file in swift:
https://github.com/apple/swift-evolution/blob/master/proposals/0034-disambiguating-line.md
And the “#line 42” syntax used by the C preprocesser is represented
with arguments like this: #sourceLocation(file: "foo", line: 42)
In the case of #tfop specifically, this is represented in the Swift
AST as an ObjectLiteralExpr, which is the normal AST node for this
sort of thing:
https://github.com/google/swift/blob/tensorflow/include/swift/AST/Expr.h#L1097
We use special lowering magic to turn it into a SIL builtin
instruction in SILGen, which are prefixed with "__tfop_"
https://github.com/google/swift/blob/tensorflow/lib/SILGen/SILGenExpr.cpp#L3009
I’d like to move away from using builtin instructions for this, and
introduce a first-class sil instruction instead, that’s tracked by:
https://github.com/google/swift/issues/16
These instructions are specially recognized by the partitioning pass
of GPE:
https://github.com/google/swift/blob/tensorflow/lib/SILOptimizer/Mandatory/TFUtilities.cpp#L715
source here
True or False?
Common Lisp has a mountain of functions. All of those functions are built (or could be built) using this small set of core functions: CAR, CDR, CONS, ATOM, EQ, QUOTE, COND, LAMBDA, LABEL, NULL.
If the answer is False, can you provide an example of a function that cannot be implemented using the core functions? Perhaps the list of core functions is incomplete, and another or two additional core functions are required?
[..] All of those functions are built (or could be built) using [..]
The important part is the could, which you already figured out yourself. That (almost*) all of (a) Lisp could be built using that small set of core functions (forms) is part of the beauty of Lisp. In practice, though, the set of functions (forms) not implemented in Lisp is a lot larger.
Why do implementations then bother to implement that much, when they only could implement that minimal core? As a small example, think of this expression:
(+ 1 2)
You could implement a Lisp that uses only the small set of core functions and it would be able (given an appropriate parser for the numbers) to evaluate this expression. But it would be painfully slow! The systems (CPUs) that are available to us mostly provide a lot of different instructions which Lisp implementations (especially the compiling ones) try to leverage as much as possible in order to allow fast execution of Lisp programs. And, to get back to the example, that also means that one wouldn't do actual calculations using peano arithmetic but rather use the "boolean logic arithmetic" implemented in hardware.
If the answer is False, can you provide an example of a function that cannot be implemented using the core functions?
That's easy, how'd you implement format? Anything that's not part of the "algorithmic" nature of a programming language, i.e. the stuff interfacing with the "outside world", is often not implemented in itself but - being dependent on the underlying system - most often in C or assembly.
False. Any function which deals with a datatype other than conses can not be implemented from your proposed core set: for instance important types such as NUMBER and SYMBOL can not be dealt with at all. Any function which does I/O likewise, and probably other vast reaches of the language.
Your list sounds like it came from some incomplete description of a proposed core of Lisp 1.5.
I am very new statistical analysis world and have taken a recent interest in the BUGS/JAGS/STAN modelling language. Something which really surprises me is that I haven't seen any examples of new functions or distributions being defined to avoid code duplication. For example, say I frequently use the square of the poisson distribution, is there anyway to do the following ?
dsqpo <- function(lambda) {
tmp ~ dpois(lambda)
tmp2 <- tmp * tmp
return(tmp2)
}
and then later on
model{
...
x ~ dsqpo(alpha)
y ~ dsqpo(beta)
}
Without defining a new temporary variable each time.
For Stan, functions will be available with the next release. The current release, v2.2.0, does not have user-defined functions as part of the language.
For the proposed syntax, see: https://github.com/stan-dev/stan/wiki/Function-Syntax-and-Semantics-Design
For additional Stan-related help, check the stan-users google group: https://groups.google.com/forum/#!forum/stan-users
In WinBUGS, OpenBUGS and JAGS, you can't define new functions as part of the modelling language. However you can do it with low-level programming in Component Pascal (for Win/OpenBUGS) or C++ (for JAGS).
For WinBUGS, see WBDev (http://www.winbugs-development.org.uk/wbdev.html). For OpenBUGS see the UDev subdirectory of the installed program, which contains a PDF manual, basically this works in the same way as WinBUGS.
For JAGS it's not properly documented - there's a user-written tutorial for adding new distributions at http://www.ncbi.nlm.nih.gov/pubmed/23959766, though nothing on functions I know of.
The recent paper "Bayesian inference with Stan: A tutorial on adding custom distributions" describes how to do this in some detail. I include the doi for a persistent link.
Reference
Annis, J., Miller, B. J., & Palmeri, T. J. (2016). Bayesian inference with Stan: A tutorial on adding custom distributions. Behavior Research Methods, 1–24. http://doi.org/10.3758/s13428-016-0746-9
I'm doing a research project on random forest algorithm. I have found numerous implementations of the algorithm but the main part of the code is often written in Fortran while I'm completely naive in it.
I have to edit the code, change the main parameters (like tree depth, num of feature variables, ...) and trace the algorithm's performance during each run.
Currently I'm using "Windows-Precompiled-RF_MexStandalone-v0.02-". The train and predict functions are matlab mex files and can not be opened or edited. Can anyone give me a piece of advice on what to do or is there a valid and completely matlab-based version of random forests.
I've read the randomforest-matlab carefully. The main training part unfortunately is a dll file. Through reading more, most of my wonders is now resolved. My question mainly was how to run several trees simultaneously.
Have you taken a look at these libraries?
Stochastic Bosque
randomforest-matlab
If you're doing a research project on it, the best thing is probably to implement the individual tree training yourself in C and then write Mex wrappers. I'd start with an ID3 tree (before attempting C4.5 for instance.) Then write the random forest code itself, which, once you write the tree code, isn't all that hard.
You'll:
learn a lot
be able to modify them as much as you like
eventually move on to exploring new areas with them
I've implemented them myself from scratch so I can help once you post some of your own code. But I don't think anybody on this site will write the code for you.
Will it take effort? Yes. Will you come out of it with more knowledge and ability than you had going in? Undoubtably.
There is a nice library in R called randomForest. It is based on the original implementation of Breiman in Fortran but it is now mainly recoded in C.
http://cran.r-project.org/web/packages/randomForest/index.html
The main parameters you talk about (tree depth, number of features to be tested, ...) are directly available.
Another library I would recommend is Weka. It is java based and lucid.Performance is slightly off though compared to R. The source code can be downloaded from http://www.cs.waikato.ac.nz/ml/weka/
I'm trying to emulate a stack in scheme. I'm using DrScheme and I select the language R5RS. I need to create functions that pop, push, and peek. But i'm having trouble figuring out how to pass by reference. I've read some information about boxes, but they are not supported in R5RS. Is there any other way to pass by reference?
Short answer: don't use r5rs; just use the native language. In current versions of DrRacket, that language is called "racket". Here's a program that uses boxes:
#lang racket
(define b (box 234))
(set-box! b 333)
(unbox b)
FWIW: Greg's answer is more purely functional than mine, but it would be a mistake to believe that mutable structures are not available in DrRacket (nee DrScheme).
Finally finally, you're misusing the term "call by reference". Boxes are just mutable structures, and a call-by-value language (such as racket, r5rs, java, etc.) can mutate these structures just fine.
Instead of passing "by reference", which is something you might do in an imperative language, Scheme encourages you to think in a functional sense. This means that your push operation, for example, would take two parameters:
a stack
a new element
and return a new stack that contains the new element in combination with the rest of the existing stack. Similarly, the pop operation would take a stack and return one with the top element gone, and peek would return the value of the top element.
As it turns out, lists in Scheme work almost exactly like stacks. The following mappings will help you get started:
push - cons
pop - rest
peek - first